File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
library/std/tests/field_projections Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ #![ allow( incomplete_features) ]
2+ #![ feature( field_projections) ]
3+
4+ use std:: field:: { Field , UnalignedField , field_of} ;
5+ use std:: mem:: offset_of;
6+ use std:: ptr;
7+
8+ pub struct Foo {
9+ pub x : usize ,
10+ pub y : usize ,
11+ pub z : ( usize , usize ) ,
12+ }
13+
14+ pub fn project_ref < F : Field > ( r : & F :: Base ) -> & F :: Type
15+ where
16+ F :: Type : Sized ,
17+ {
18+ unsafe { & * ptr:: from_ref ( r) . byte_add ( F :: OFFSET ) . cast ( ) }
19+ }
20+
21+ #[ test]
22+ fn foo ( ) {
23+ let foo = Foo { x : 42 , y : 24 , z : ( 43 , 44 ) } ;
24+ let x = project_ref :: < field_of ! ( Foo , x) > ( & foo) ;
25+ let y = project_ref :: < field_of ! ( Foo , y) > ( & foo) ;
26+ let z0 = project_ref :: < field_of ! ( Foo , z. 0 ) > ( & foo) ;
27+ let z1 = project_ref :: < field_of ! ( Foo , z. 1 ) > ( & foo) ;
28+ assert_eq ! ( * x, 42 ) ;
29+ assert_eq ! ( * y, 24 ) ;
30+ assert_eq ! ( <field_of!( Foo , x) >:: OFFSET , offset_of!( Foo , x) ) ;
31+ assert_eq ! ( * z0, 43 ) ;
32+ assert_eq ! ( * z1, 44 ) ;
33+ }
34+
35+ fn main ( ) { }
You can’t perform that action at this time.
0 commit comments