@@ -43,48 +43,53 @@ mod primitive {
43
43
pub struct PushBytes ( [ u8 ] ) ;
44
44
45
45
impl PushBytes {
46
- /// Creates `&Self ` without checking the length.
46
+ /// Creates `&PushBytes ` without checking the length.
47
47
///
48
48
/// # Safety
49
49
///
50
- /// The caller is responsible for checking that the length is less than the [`LIMIT`] .
50
+ /// The caller is responsible for checking that the length is less than the 2^32 .
51
51
unsafe fn from_slice_unchecked ( bytes : & [ u8 ] ) -> & Self {
52
+ // SAFETY: The caller must guarantee that bytes.len() < 2^32.
53
+ // If that is the case the conversion is sound because &[u8] and &PushBytes
54
+ // have the same layout (because of #[repr(transparent)] on PushBytes).
52
55
& * ( bytes as * const [ u8 ] as * const PushBytes )
53
56
}
54
57
55
- /// Creates `&mut Self ` without checking the length.
58
+ /// Creates `&mut PushBytes ` without checking the length.
56
59
///
57
60
/// # Safety
58
61
///
59
- /// The caller is responsible for checking that the length is less than the [`LIMIT`] .
62
+ /// The caller is responsible for checking that the length is less than the 2^32 .
60
63
unsafe fn from_mut_slice_unchecked ( bytes : & mut [ u8 ] ) -> & mut Self {
64
+ // SAFETY: The caller must guarantee that bytes.len() < 2^32.
65
+ // If that is the case the conversion is sound because &mut [u8] and &mut PushBytes
66
+ // have the same layout (because of #[repr(transparent)] on PushBytes).
61
67
& mut * ( bytes as * mut [ u8 ] as * mut PushBytes )
62
68
}
63
69
64
- /// Creates an empty `PushBytes`.
70
+ /// Creates an empty `& PushBytes`.
65
71
pub fn empty ( ) -> & ' static Self {
66
- // 0 < LIMIT
72
+ // SAFETY: 0 < 2^32.
67
73
unsafe { Self :: from_slice_unchecked ( & [ ] ) }
68
74
}
69
75
70
76
/// Returns the underlying bytes.
71
77
pub fn as_bytes ( & self ) -> & [ u8 ] { & self . 0 }
72
78
73
- /// Returns the underlying mutbale bytes.
79
+ /// Returns the underlying mutable bytes.
74
80
pub fn as_mut_bytes ( & mut self ) -> & mut [ u8 ] { & mut self . 0 }
75
81
}
76
82
77
83
macro_rules! delegate_index {
78
84
( $( $type: ty) ,* $( , ) ?) => {
79
85
$(
80
- /// Script subslicing operation - read [slicing safety](#slicing-safety)!
81
86
impl Index <$type> for PushBytes {
82
87
type Output = Self ;
83
88
84
89
#[ inline]
85
90
#[ track_caller]
86
91
fn index( & self , index: $type) -> & Self :: Output {
87
- // Slicing can not make slices longer
92
+ // SAFETY: Slicing can not make slices longer.
88
93
unsafe {
89
94
Self :: from_slice_unchecked( & self . 0 [ index] )
90
95
}
@@ -117,7 +122,7 @@ mod primitive {
117
122
118
123
fn try_from ( bytes : & ' a [ u8 ] ) -> Result < Self , Self :: Error > {
119
124
check_limit ( bytes. len ( ) ) ?;
120
- // We've just checked the length
125
+ // SAFETY: We've just checked the length.
121
126
Ok ( unsafe { PushBytes :: from_slice_unchecked ( bytes) } )
122
127
}
123
128
}
@@ -127,7 +132,7 @@ mod primitive {
127
132
128
133
fn try_from ( bytes : & ' a mut [ u8 ] ) -> Result < Self , Self :: Error > {
129
134
check_limit ( bytes. len ( ) ) ?;
130
- // We've just checked the length
135
+ // SAFETY: We've just checked the length.
131
136
Ok ( unsafe { PushBytes :: from_mut_slice_unchecked ( bytes) } )
132
137
}
133
138
}
@@ -139,15 +144,15 @@ mod primitive {
139
144
fn from( bytes: & ' a [ u8 ; $len] ) -> Self {
140
145
// Check that the macro wasn't called with a wrong number.
141
146
const _: ( ) = [ ( ) ; 1 ] [ ( $len >= 0x100000000u64 ) as usize ] ;
142
- // We know the size of array statically and we checked macro input.
147
+ // SAFETY: We know the size of array statically and we checked macro input.
143
148
unsafe { PushBytes :: from_slice_unchecked( bytes) }
144
149
}
145
150
}
146
151
147
152
impl <' a> From <& ' a mut [ u8 ; $len] > for & ' a mut PushBytes {
148
153
fn from( bytes: & ' a mut [ u8 ; $len] ) -> Self {
149
154
// Macro check already above, no need to duplicate.
150
- // We know the size of array statically and we checked macro input.
155
+ // SAFETY: We know the size of array statically and we checked macro input.
151
156
unsafe { PushBytes :: from_mut_slice_unchecked( bytes) }
152
157
}
153
158
}
0 commit comments