@@ -17,18 +17,10 @@ include!("include.rs");
1717
1818#[ test]
1919fn zst ( ) {
20- use imp:: From ;
21-
22- // FIXME(#5): Use `try_transmute` in this test once it's available.
23- let candidate = :: zerocopy:: Ptr :: from_ref ( <& imp:: ReadOnly < _ > >:: from ( & ( ( ) ) ) ) ;
24- let candidate = candidate. forget_aligned ( ) ;
25- // SAFETY: `&()` trivially consists entirely of initialized bytes.
26- let candidate = unsafe { candidate. assume_initialized ( ) } ;
27- let is_bit_valid = <( ) as imp:: TryFromBytes >:: is_bit_valid ( candidate) ;
28- imp:: assert!( is_bit_valid) ;
20+ crate :: util:: test_is_bit_valid :: < ( ) > ( ( ) , true ) ;
2921}
3022
31- #[ derive( imp:: TryFromBytes , imp:: Immutable ) ]
23+ #[ derive( imp:: TryFromBytes , imp:: Immutable , imp :: IntoBytes ) ]
3224#[ repr( C ) ]
3325struct One {
3426 a : u8 ,
@@ -38,18 +30,11 @@ util_assert_impl_all!(One: imp::TryFromBytes);
3830
3931#[ test]
4032fn one ( ) {
41- use imp:: From ;
42-
43- // FIXME(#5): Use `try_transmute` in this test once it's available.
44- let candidate = :: zerocopy:: Ptr :: from_ref ( <& imp:: ReadOnly < _ > >:: from ( & One { a : 42 } ) ) ;
45- let candidate = candidate. forget_aligned ( ) ;
46- // SAFETY: `&One` consists entirely of initialized bytes.
47- let candidate = unsafe { candidate. assume_initialized ( ) } ;
48- let is_bit_valid = <One as imp:: TryFromBytes >:: is_bit_valid ( candidate) ;
49- imp:: assert!( is_bit_valid) ;
33+ crate :: util:: test_is_bit_valid :: < One > ( One { a : 42 } , true ) ;
34+ crate :: util:: test_is_bit_valid :: < One > ( One { a : 43 } , true ) ;
5035}
5136
52- #[ derive( imp:: TryFromBytes , imp:: Immutable ) ]
37+ #[ derive( imp:: TryFromBytes , imp:: Immutable , imp :: IntoBytes ) ]
5338#[ repr( C ) ]
5439struct Two {
5540 a : bool ,
@@ -60,36 +45,9 @@ util_assert_impl_all!(Two: imp::TryFromBytes);
6045
6146#[ test]
6247fn two ( ) {
63- use imp:: From ;
64-
65- // FIXME(#5): Use `try_transmute` in this test once it's available.
66- let candidate = :: zerocopy:: Ptr :: from_ref ( <& imp:: ReadOnly < _ > >:: from ( & Two { a : false , b : ( ) } ) ) ;
67- let candidate = candidate. forget_aligned ( ) ;
68- // SAFETY: `&Two` consists entirely of initialized bytes.
69- let candidate = unsafe { candidate. assume_initialized ( ) } ;
70- let is_bit_valid = <Two as imp:: TryFromBytes >:: is_bit_valid ( candidate) ;
71- imp:: assert!( is_bit_valid) ;
72- }
73-
74- #[ test]
75- fn two_bad ( ) {
76- // FIXME(#5): Use `try_transmute` in this test once it's available.
77- let mut buf = [ 2u8 ; 1 ] ;
78- let candidate = :: zerocopy:: Ptr :: from_mut ( & mut buf) ;
79- let candidate = candidate. forget_aligned ( ) ;
80- // SAFETY: `&Two` consists entirely of initialized bytes.
81- let candidate = unsafe { candidate. assume_initialized ( ) } ;
82-
83- let candidate = {
84- use imp:: pointer:: { cast:: CastSized , BecauseExclusive } ;
85- candidate. cast :: < _ , CastSized , ( _ , BecauseExclusive ) > ( )
86- } ;
87-
88- // SAFETY: `candidate`'s referent is as-initialized as `Two`.
89- let mut candidate = unsafe { candidate. assume_initialized ( ) } ;
90-
91- let is_bit_valid = <Two as imp:: TryFromBytes >:: is_bit_valid ( candidate. reborrow_shared ( ) ) ;
92- imp:: assert!( !is_bit_valid) ;
48+ crate :: util:: test_is_bit_valid :: < Two > ( Two { a : false , b : ( ) } , true ) ;
49+ crate :: util:: test_is_bit_valid :: < Two > ( Two { a : true , b : ( ) } , true ) ;
50+ crate :: util:: test_is_bit_valid :: < Two > ( [ 2u8 ] , false ) ;
9351}
9452
9553#[ derive( imp:: KnownLayout , imp:: TryFromBytes ) ]
@@ -105,17 +63,14 @@ fn un_sized() {
10563 // FIXME(#5): Use `try_transmute` in this test once it's available.
10664 let mut buf = [ 16u8 , 12 , 42 ] ;
10765 let candidate = :: zerocopy:: Ptr :: from_mut ( & mut buf[ ..] ) ;
108- let candidate = candidate. forget_aligned ( ) ;
10966 // SAFETY: `&Unsized` consists entirely of initialized bytes.
11067 let candidate = unsafe { candidate. assume_initialized ( ) } ;
11168
112- let candidate = {
69+ let mut candidate = {
11370 use imp:: pointer:: { cast:: CastUnsized , BecauseExclusive } ;
11471 candidate. cast :: < _ , CastUnsized , ( _ , BecauseExclusive ) > ( )
11572 } ;
11673
117- // SAFETY: `candidate`'s referent is as-initialized as `Two`.
118- let mut candidate = unsafe { candidate. assume_initialized ( ) } ;
11974 let is_bit_valid = <Unsized as imp:: TryFromBytes >:: is_bit_valid ( candidate. reborrow_shared ( ) ) ;
12075 imp:: assert!( is_bit_valid) ;
12176}
@@ -151,7 +106,7 @@ where
151106
152107util_assert_impl_all ! ( WithParams <' static , ' static , u8 , 42 >: imp:: TryFromBytes ) ;
153108
154- #[ derive( imp:: FromBytes ) ]
109+ #[ derive( imp:: FromBytes , imp :: IntoBytes ) ]
155110#[ repr( C ) ]
156111struct MaybeFromBytes < T > ( T ) ;
157112
@@ -161,23 +116,9 @@ fn test_maybe_from_bytes() {
161116 // trivial `is_bit_valid` impl that always returns true. This test confirms
162117 // that we *don't* spuriously do that when generic parameters are present.
163118
164- let mut buf = [ 2u8 ] ;
165- let candidate = :: zerocopy:: Ptr :: from_mut ( & mut buf) ;
166- let candidate = candidate. bikeshed_recall_initialized_from_bytes ( ) ;
167-
168- let candidate = {
169- use imp:: pointer:: { cast:: CastSized , BecauseExclusive } ;
170- candidate. cast :: < MaybeFromBytes < bool > , CastSized , ( _ , BecauseExclusive ) > ( )
171- } ;
172-
173- // SAFETY: `[u8]` consists entirely of initialized bytes.
174- let candidate = unsafe { candidate. assume_initialized ( ) } ;
175- let mut candidate =
176- candidate. cast :: < _ , imp:: pointer:: cast:: CastSized , ( _ , imp:: pointer:: BecauseExclusive ) > ( ) ;
177-
178- let is_bit_valid =
179- <MaybeFromBytes < bool > as imp:: TryFromBytes >:: is_bit_valid ( candidate. reborrow_shared ( ) ) ;
180- imp:: assert!( !is_bit_valid) ;
119+ crate :: util:: test_is_bit_valid :: < MaybeFromBytes < bool > > ( MaybeFromBytes ( false ) , true ) ;
120+ crate :: util:: test_is_bit_valid :: < MaybeFromBytes < bool > > ( MaybeFromBytes ( true ) , true ) ;
121+ crate :: util:: test_is_bit_valid :: < MaybeFromBytes < bool > > ( [ 2u8 ] , false ) ;
181122}
182123
183124#[ derive( Debug , PartialEq , Eq , imp:: TryFromBytes , imp:: Immutable , imp:: KnownLayout ) ]
0 commit comments