@@ -26,7 +26,7 @@ pub mod external {
2626 unsafe {
2727 std:: slice:: from_raw_parts (
2828 ( * self . inner ) . code . cast ( ) ,
29- ( * self . inner ) . size * std :: mem :: size_of :: < u32 > ( ) ,
29+ ( * self . inner ) . size * size_of :: < u32 > ( ) ,
3030 )
3131 }
3232 }
@@ -44,7 +44,7 @@ pub mod external {
4444
4545pub enum Binary {
4646 #[ cfg( feature = "use-compiled-tools" ) ]
47- External ( self :: external:: ExternalBinary ) ,
47+ External ( external:: ExternalBinary ) ,
4848 OwnedU32 ( Vec < u32 > ) ,
4949 OwnedU8 ( Vec < u8 > ) ,
5050}
@@ -63,12 +63,12 @@ impl Binary {
6363 }
6464}
6565
66- impl std :: convert :: TryFrom < Vec < u8 > > for Binary {
66+ impl TryFrom < Vec < u8 > > for Binary {
6767 type Error = crate :: Error ;
6868
6969 #[ inline]
7070 fn try_from ( v : Vec < u8 > ) -> Result < Self , Self :: Error > {
71- if v. len ( ) % std :: mem :: size_of :: < u32 > ( ) != 0 {
71+ if v. len ( ) % size_of :: < u32 > ( ) != 0 {
7272 Err ( crate :: Error {
7373 inner : spirv_tools_sys:: shared:: SpirvResult :: InvalidBinary ,
7474 diagnostic : None ,
@@ -125,25 +125,30 @@ impl fmt::Debug for Binary {
125125/// digestible byte array
126126#[ inline]
127127pub fn from_binary ( bin : & [ u32 ] ) -> & [ u8 ] {
128- unsafe { std:: slice:: from_raw_parts ( bin. as_ptr ( ) . cast ( ) , std :: mem :: size_of_val ( bin) ) }
128+ unsafe { std:: slice:: from_raw_parts ( bin. as_ptr ( ) . cast ( ) , size_of_val ( bin) ) }
129129}
130130
131- /// Transmutes a regular byte array into a SPIRV binary of 32 bit words. This
132- /// will fail if the input is not `% sizeof(u32)`
131+ /// Transmutes a regular byte array into a SPIRV binary of 32 bit words.
132+ /// Fails if the input is not `input.as_ptr() % 4` and `input.len() % 4`.
133133#[ inline]
134134pub fn to_binary ( bytes : & [ u8 ] ) -> Result < & [ u32 ] , crate :: Error > {
135- if bytes. len ( ) % std:: mem:: size_of :: < u32 > ( ) != 0 {
135+ if bytes. len ( ) % size_of :: < u32 > ( ) != 0 {
136+ return Err ( crate :: Error {
137+ inner : spirv_tools_sys:: shared:: SpirvResult :: InvalidBinary ,
138+ diagnostic : None ,
139+ } ) ;
140+ }
141+ if bytes. as_ptr ( ) . addr ( ) % size_of :: < u32 > ( ) != 0 {
136142 return Err ( crate :: Error {
137143 inner : spirv_tools_sys:: shared:: SpirvResult :: InvalidBinary ,
138144 diagnostic : None ,
139145 } ) ;
140146 }
141147
142148 #[ allow( clippy:: size_of_in_element_count) ]
143- Ok ( unsafe {
144- std:: slice:: from_raw_parts (
145- bytes. as_ptr ( ) . cast ( ) ,
146- bytes. len ( ) / std:: mem:: size_of :: < u32 > ( ) ,
147- )
148- } )
149+ Ok (
150+ unsafe {
151+ std:: slice:: from_raw_parts ( bytes. as_ptr ( ) . cast ( ) , bytes. len ( ) / size_of :: < u32 > ( ) )
152+ } ,
153+ )
149154}
0 commit comments