@@ -41,6 +41,7 @@ use as_slice::{AsMutSlice, AsSlice};
4141/// A marker trait for an alignment value.
4242pub trait Alignment : Copy + sealed:: Sealed { }
4343
44+ impl Alignment for A1 { }
4445impl Alignment for A2 { }
4546impl Alignment for A4 { }
4647impl Alignment for A8 { }
@@ -51,6 +52,7 @@ impl Alignment for A64 {}
5152mod sealed {
5253 pub trait Sealed { }
5354
55+ impl Sealed for super :: A1 { }
5456 impl Sealed for super :: A2 { }
5557 impl Sealed for super :: A4 { }
5658 impl Sealed for super :: A8 { }
@@ -59,6 +61,11 @@ mod sealed {
5961 impl Sealed for super :: A64 { }
6062}
6163
64+ /// 1-byte alignment
65+ #[ derive( Clone , Copy ) ]
66+ #[ repr( align( 1 ) ) ]
67+ pub struct A1 ;
68+
6269/// 2-byte alignment
6370#[ derive( Clone , Copy ) ]
6471#[ repr( align( 2 ) ) ]
@@ -267,50 +274,59 @@ where
267274fn sanity ( ) {
268275 use core:: mem;
269276
277+ let a: Aligned < A1 , _ > = Aligned ( [ 0u8 ; 3 ] ) ;
270278 let x: Aligned < A2 , _ > = Aligned ( [ 0u8 ; 3 ] ) ;
271279 let y: Aligned < A4 , _ > = Aligned ( [ 0u8 ; 3 ] ) ;
272280 let z: Aligned < A8 , _ > = Aligned ( [ 0u8 ; 3 ] ) ;
273281 let w: Aligned < A16 , _ > = Aligned ( [ 0u8 ; 3 ] ) ;
274282
275283 // check alignment
284+ assert_eq ! ( mem:: align_of_val( & a) , 1 ) ;
276285 assert_eq ! ( mem:: align_of_val( & x) , 2 ) ;
277286 assert_eq ! ( mem:: align_of_val( & y) , 4 ) ;
278287 assert_eq ! ( mem:: align_of_val( & z) , 8 ) ;
279288 assert_eq ! ( mem:: align_of_val( & w) , 16 ) ;
280289
290+ assert ! ( a. as_ptr( ) as usize % 1 == 0 ) ;
281291 assert ! ( x. as_ptr( ) as usize % 2 == 0 ) ;
282292 assert ! ( y. as_ptr( ) as usize % 4 == 0 ) ;
283293 assert ! ( z. as_ptr( ) as usize % 8 == 0 ) ;
284294 assert ! ( w. as_ptr( ) as usize % 16 == 0 ) ;
285295
286296 // test `deref`
297+ assert_eq ! ( a. len( ) , 3 ) ;
287298 assert_eq ! ( x. len( ) , 3 ) ;
288299 assert_eq ! ( y. len( ) , 3 ) ;
289300 assert_eq ! ( z. len( ) , 3 ) ;
290301 assert_eq ! ( w. len( ) , 3 ) ;
291302
292303 // alignment should be preserved after slicing
304+ let a: & Aligned < _ , [ _ ] > = & a;
293305 let x: & Aligned < _ , [ _ ] > = & x;
294306 let y: & Aligned < _ , [ _ ] > = & y;
295307 let z: & Aligned < _ , [ _ ] > = & z;
296308 let w: & Aligned < _ , [ _ ] > = & w;
297309
310+ let a: & Aligned < _ , _ > = & a[ ..2 ] ;
298311 let x: & Aligned < _ , _ > = & x[ ..2 ] ;
299312 let y: & Aligned < _ , _ > = & y[ ..2 ] ;
300313 let z: & Aligned < _ , _ > = & z[ ..2 ] ;
301314 let w: & Aligned < _ , _ > = & w[ ..2 ] ;
302315
316+ assert ! ( a. as_ptr( ) as usize % 1 == 0 ) ;
303317 assert ! ( x. as_ptr( ) as usize % 2 == 0 ) ;
304318 assert ! ( y. as_ptr( ) as usize % 4 == 0 ) ;
305319 assert ! ( z. as_ptr( ) as usize % 8 == 0 ) ;
306320 assert ! ( w. as_ptr( ) as usize % 16 == 0 ) ;
307321
308322 // alignment should be preserved after boxing
323+ let a: Box < Aligned < A1 , [ u8 ] > > = Box :: new ( Aligned ( [ 0u8 ; 3 ] ) ) ;
309324 let x: Box < Aligned < A2 , [ u8 ] > > = Box :: new ( Aligned ( [ 0u8 ; 3 ] ) ) ;
310325 let y: Box < Aligned < A4 , [ u8 ] > > = Box :: new ( Aligned ( [ 0u8 ; 3 ] ) ) ;
311326 let z: Box < Aligned < A8 , [ u8 ] > > = Box :: new ( Aligned ( [ 0u8 ; 3 ] ) ) ;
312327 let w: Box < Aligned < A16 , [ u8 ] > > = Box :: new ( Aligned ( [ 0u8 ; 3 ] ) ) ;
313328
329+ assert_eq ! ( mem:: align_of_val( & * a) , 1 ) ;
314330 assert_eq ! ( mem:: align_of_val( & * x) , 2 ) ;
315331 assert_eq ! ( mem:: align_of_val( & * y) , 4 ) ;
316332 assert_eq ! ( mem:: align_of_val( & * z) , 8 ) ;
0 commit comments