@@ -68,6 +68,7 @@ extern crate serde_json;
68
68
#[ cfg( all( test, feature = "serde" ) ) ]
69
69
extern crate serde_test;
70
70
71
+ use alloc:: alloc:: Opaque ;
71
72
use alloc:: allocator:: Alloc ;
72
73
use std:: sync:: atomic;
73
74
use std:: sync:: atomic:: Ordering :: { Acquire , Relaxed , Release , SeqCst } ;
@@ -154,7 +155,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
154
155
/// }
155
156
/// ```
156
157
pub struct ArcCStr {
157
- ptr : NonNull < u8 > ,
158
+ ptr : NonNull < Opaque > ,
158
159
}
159
160
160
161
use std:: ffi:: FromBytesWithNulError ;
@@ -215,12 +216,10 @@ impl ArcCStr {
215
216
let sz = aus + buf. len ( ) + 1 ;
216
217
let aul = alloc:: allocator:: Layout :: from_size_align ( sz, aual) . unwrap ( ) ;
217
218
218
- let mut s = ptr:: Unique :: new (
219
- alloc:: heap:: Heap
220
- . alloc ( aul)
221
- . expect ( "could not allocate memory" ) ,
222
- ) . unwrap ( ) ;
223
- let cstr = s. as_ptr ( ) . offset ( aus as isize ) ;
219
+ let mut s = alloc:: alloc:: Global
220
+ . alloc ( aul)
221
+ . expect ( "could not allocate memory" ) ;
222
+ let cstr = ( s. as_ptr ( ) as * mut u8 ) . offset ( aus as isize ) ;
224
223
// initialize the AtomicUsize to 1
225
224
{
226
225
let atom: & mut atomic:: AtomicUsize = mem:: transmute ( s. as_mut ( ) ) ;
@@ -231,9 +230,7 @@ impl ArcCStr {
231
230
// add \0 terminator
232
231
* cstr. offset ( buf. len ( ) as isize ) = 0u8 ;
233
232
// and we're all good
234
- ArcCStr {
235
- ptr : NonNull :: new ( s. as_ptr ( ) . offset ( 0 ) ) . unwrap ( ) ,
236
- }
233
+ ArcCStr { ptr : s }
237
234
}
238
235
239
236
/// Gets the number of pointers to this string.
@@ -283,7 +280,7 @@ impl ArcCStr {
283
280
size_of :: < atomic:: AtomicUsize > ( ) + blen,
284
281
align_of :: < atomic:: AtomicUsize > ( ) ,
285
282
) . unwrap ( ) ;
286
- alloc:: heap :: Heap . dealloc ( self . ptr . as_ptr ( ) . offset ( 0 ) as * mut _ , aul)
283
+ alloc:: alloc :: Global . dealloc ( self . ptr , aul)
287
284
}
288
285
289
286
#[ inline]
@@ -305,7 +302,7 @@ impl ArcCStr {
305
302
/// assert!(!ArcCStr::ptr_eq(&five, &other_five));
306
303
/// ```
307
304
pub fn ptr_eq ( this : & Self , other : & Self ) -> bool {
308
- unsafe { this. ptr . as_ptr ( ) . offset ( 0 ) == other. ptr . as_ptr ( ) . offset ( 0 ) }
305
+ this. ptr == other. ptr
309
306
}
310
307
}
311
308
@@ -375,7 +372,7 @@ impl Deref for ArcCStr {
375
372
// place.
376
373
//
377
374
let aus = size_of :: < atomic:: AtomicUsize > ( ) as isize ;
378
- unsafe { CStr :: from_ptr ( mem:: transmute ( self . ptr . as_ptr ( ) . offset ( aus) ) ) }
375
+ unsafe { CStr :: from_ptr ( mem:: transmute ( ( self . ptr . as_ptr ( ) as * mut u8 ) . offset ( aus) ) ) }
379
376
}
380
377
}
381
378
@@ -615,7 +612,9 @@ impl serde::Serialize for ArcCStr {
615
612
// once to find the length, then once more to serialize...
616
613
let aus = size_of :: < atomic:: AtomicUsize > ( ) ;
617
614
let len = self . to_bytes ( ) . len ( ) ;
618
- let bytes = unsafe { slice:: from_raw_parts ( self . ptr . as_ptr ( ) . offset ( aus as isize ) , len) } ;
615
+ let bytes = unsafe {
616
+ slice:: from_raw_parts ( ( self . ptr . as_ptr ( ) as * mut u8 ) . offset ( aus as isize ) , len)
617
+ } ;
619
618
serializer. serialize_bytes ( bytes)
620
619
}
621
620
}
0 commit comments