@@ -25,69 +25,68 @@ pub trait Initializable {
2525 fn is_initialized ( & self ) -> bool ;
2626}
2727
28- /// Trait for `RawType`s that can be *viewed* from a byte slice.
29- pub trait Viewable < T : Initializable + RawType > {
30- /// Return a reference for an initialized `T` from the given bytes.
31- ///
32- /// # Safety
33- ///
34- /// The caller must ensure that `bytes` contains a valid representation of `T`.
35- #[ inline( always) ]
36- unsafe fn load ( bytes : & [ u8 ] ) -> Result < & T , ProgramError > {
37- Self :: load_unchecked ( bytes) . and_then ( |t| {
38- // checks if the data is initialized
39- if t. is_initialized ( ) {
40- Ok ( t)
41- } else {
42- Err ( ProgramError :: UninitializedAccount )
43- }
44- } )
45- }
46-
47- /// Return a `T` reference from the given bytes.
48- ///
49- /// This function does not check if the data is initialized.
50- ///
51- /// # Safety
52- ///
53- /// The caller must ensure that `bytes` contains a valid representation of `T`.
54- #[ inline( always) ]
55- unsafe fn load_unchecked ( bytes : & [ u8 ] ) -> Result < & T , ProgramError > {
56- if bytes. len ( ) != T :: LEN {
57- return Err ( ProgramError :: InvalidAccountData ) ;
28+ /// Return a reference for an initialized `T` from the given bytes.
29+ ///
30+ /// # Safety
31+ ///
32+ /// The caller must ensure that `bytes` contains a valid representation of `T`.
33+ #[ inline( always) ]
34+ pub unsafe fn load < T : Initializable + RawType > ( bytes : & [ u8 ] ) -> Result < & T , ProgramError > {
35+ load_unchecked ( bytes) . and_then ( |t : & T | {
36+ // checks if the data is initialized
37+ if t. is_initialized ( ) {
38+ Ok ( t)
39+ } else {
40+ Err ( ProgramError :: UninitializedAccount )
5841 }
59- Ok ( & * ( bytes . as_ptr ( ) as * const T ) )
60- }
42+ } )
43+ }
6144
62- /// Return a mutable reference for an initialized `T` from the given bytes.
63- ///
64- /// # Safety
65- ///
66- /// The caller must ensure that `bytes` contains a valid representation of `T`.
67- #[ inline( always) ]
68- unsafe fn load_mut ( bytes : & mut [ u8 ] ) -> Result < & mut T , ProgramError > {
69- Self :: load_mut_unchecked ( bytes) . and_then ( |t| {
70- // checks if the data is initialized
71- if t. is_initialized ( ) {
72- Ok ( t)
73- } else {
74- Err ( ProgramError :: UninitializedAccount )
75- }
76- } )
45+ /// Return a `T` reference from the given bytes.
46+ ///
47+ /// This function does not check if the data is initialized.
48+ ///
49+ /// # Safety
50+ ///
51+ /// The caller must ensure that `bytes` contains a valid representation of `T`.
52+ #[ inline( always) ]
53+ pub unsafe fn load_unchecked < T : RawType > ( bytes : & [ u8 ] ) -> Result < & T , ProgramError > {
54+ if bytes. len ( ) != T :: LEN {
55+ return Err ( ProgramError :: InvalidAccountData ) ;
7756 }
57+ Ok ( & * ( bytes. as_ptr ( ) as * const T ) )
58+ }
7859
79- /// Return a mutable `T` reference from the given bytes.
80- ///
81- /// This function does not check if the data is initialized.
82- ///
83- /// # Safety
84- ///
85- /// The caller must ensure that `bytes` contains a valid representation of `T`.
86- #[ inline( always) ]
87- unsafe fn load_mut_unchecked ( bytes : & mut [ u8 ] ) -> Result < & mut T , ProgramError > {
88- if bytes. len ( ) != T :: LEN {
89- return Err ( ProgramError :: InvalidAccountData ) ;
60+ /// Return a mutable reference for an initialized `T` from the given bytes.
61+ ///
62+ /// # Safety
63+ ///
64+ /// The caller must ensure that `bytes` contains a valid representation of `T`.
65+ #[ inline( always) ]
66+ pub unsafe fn load_mut < T : Initializable + RawType > (
67+ bytes : & mut [ u8 ] ,
68+ ) -> Result < & mut T , ProgramError > {
69+ load_mut_unchecked ( bytes) . and_then ( |t : & mut T | {
70+ // checks if the data is initialized
71+ if t. is_initialized ( ) {
72+ Ok ( t)
73+ } else {
74+ Err ( ProgramError :: UninitializedAccount )
9075 }
91- Ok ( & mut * ( bytes. as_mut_ptr ( ) as * mut T ) )
76+ } )
77+ }
78+
79+ /// Return a mutable `T` reference from the given bytes.
80+ ///
81+ /// This function does not check if the data is initialized.
82+ ///
83+ /// # Safety
84+ ///
85+ /// The caller must ensure that `bytes` contains a valid representation of `T`.
86+ #[ inline( always) ]
87+ pub unsafe fn load_mut_unchecked < T : RawType > ( bytes : & mut [ u8 ] ) -> Result < & mut T , ProgramError > {
88+ if bytes. len ( ) != T :: LEN {
89+ return Err ( ProgramError :: InvalidAccountData ) ;
9290 }
91+ Ok ( & mut * ( bytes. as_mut_ptr ( ) as * mut T ) )
9392}
0 commit comments