@@ -101,27 +101,28 @@ impl AsMut<[u8]> for BytesMut {
101
101
///
102
102
/// * `buf` - Must be a valid pointer to an array of bytes
103
103
/// * `len` - Length of buffer, `buf[len-1]` must be a valid dereference
104
+ ///
105
+ /// SAFTEY: buf must not be NULL and point to a valid memory area of size `len`.
104
106
#[ no_mangle]
105
- pub extern "C" fn rust_util_bytes ( buf : * const c_uchar , len : usize ) -> Bytes {
107
+ pub unsafe extern "C" fn rust_util_bytes ( buf : * const c_uchar , len : usize ) -> Bytes {
106
108
Bytes { buf, len }
107
109
}
108
110
109
111
/// Convert buffer to mutable slice
110
112
///
111
113
/// * `buf` - Must be a valid pointer to an array of bytes
112
114
/// * `len` - Length of buffer, `buf[len-1]` must be a valid dereference
115
+ ///
116
+ /// SAFTEY: buf must not be NULL and point to a valid memory area of size `len`.
113
117
#[ no_mangle]
114
118
pub unsafe extern "C" fn rust_util_bytes_mut ( buf : * mut c_uchar , len : usize ) -> BytesMut {
115
119
BytesMut { buf, len }
116
120
}
117
121
118
122
/// Base58Check-encode the input.
119
- ///
120
- /// #Safety
121
- /// buf and out must not be NULL and point to valid memory areas.
122
123
#[ cfg( feature = "c-unit-testing" ) ]
123
124
#[ no_mangle]
124
- pub unsafe extern "C" fn rust_base58_encode_check ( buf : Bytes , mut out : BytesMut ) -> bool {
125
+ pub extern "C" fn rust_base58_encode_check ( buf : Bytes , mut out : BytesMut ) -> bool {
125
126
if buf. len == 0 {
126
127
return false ;
127
128
}
@@ -168,36 +169,36 @@ mod tests {
168
169
#[ should_panic]
169
170
fn create_invalid_bytes_ref ( ) {
170
171
// Calling `as_ref()` will panic because it tries to create an invalid rust slice.
171
- rust_util_bytes ( core:: ptr:: null ( ) , 1 ) . as_ref ( ) ;
172
+ ( unsafe { rust_util_bytes ( core:: ptr:: null ( ) , 1 ) } ) . as_ref ( ) ;
172
173
}
173
174
174
175
#[ test]
175
176
fn test_uint8_to_hex ( ) {
176
177
let buf = [ 1u8 , 2 , 3 , 14 , 15 , 255 ] ;
177
178
let mut string = String :: from ( "xxxxxxxxxxxxx" ) ;
178
- rust_util_uint8_to_hex ( rust_util_bytes ( buf. as_ptr ( ) , buf. len ( ) ) , unsafe {
179
- rust_util_bytes_mut ( string. as_mut_ptr ( ) , string. len ( ) )
180
- } ) ;
179
+ rust_util_uint8_to_hex (
180
+ unsafe { rust_util_bytes ( buf. as_ptr ( ) , buf. len ( ) ) } ,
181
+ unsafe { rust_util_bytes_mut ( string. as_mut_ptr ( ) , string. len ( ) ) } ,
182
+ ) ;
181
183
assert_eq ! ( string, "0102030e0fff\0 " ) ;
182
184
183
185
// Bigger buffer also works.
184
186
let mut string = String :: from ( "\0 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ) ;
185
- rust_util_uint8_to_hex ( rust_util_bytes ( buf. as_ptr ( ) , buf. len ( ) ) , unsafe {
186
- rust_util_bytes_mut ( string. as_mut_ptr ( ) , string. len ( ) )
187
- } ) ;
187
+ rust_util_uint8_to_hex (
188
+ unsafe { rust_util_bytes ( buf. as_ptr ( ) , buf. len ( ) ) } ,
189
+ unsafe { rust_util_bytes_mut ( string. as_mut_ptr ( ) , string. len ( ) ) } ,
190
+ ) ;
188
191
assert_eq ! ( string, "0102030e0fff\0 xxxxxxxxxxxxxxxxxxxxxxx" ) ;
189
192
}
190
193
191
194
#[ test]
192
195
fn test_rust_base58_encode_check ( ) {
193
196
let buf = b"test" ;
194
197
let mut result_buf = [ 0u8 ; 100 ] ;
195
- assert ! ( unsafe {
196
- rust_base58_encode_check(
197
- rust_util_bytes( buf. as_ptr( ) , buf. len( ) ) ,
198
- rust_util_bytes_mut( result_buf. as_mut_ptr( ) , result_buf. len( ) ) ,
199
- )
200
- } ) ;
198
+ assert ! ( rust_base58_encode_check(
199
+ unsafe { rust_util_bytes( buf. as_ptr( ) , buf. len( ) ) } ,
200
+ unsafe { rust_util_bytes_mut( result_buf. as_mut_ptr( ) , result_buf. len( ) ) } ,
201
+ ) ) ;
201
202
let expected = b"LUC1eAJa5jW\0 " ;
202
203
assert_eq ! ( & result_buf[ ..expected. len( ) ] , expected) ;
203
204
}
0 commit comments