Skip to content

Commit de0534a

Browse files
committed
New nightly alloc API rust-lang/rust#49669
1 parent e58924e commit de0534a

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

src/lib.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern crate serde_json;
6868
#[cfg(all(test, feature = "serde"))]
6969
extern crate serde_test;
7070

71+
use alloc::alloc::Opaque;
7172
use alloc::allocator::Alloc;
7273
use std::sync::atomic;
7374
use std::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
@@ -154,7 +155,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
154155
/// }
155156
/// ```
156157
pub struct ArcCStr {
157-
ptr: NonNull<u8>,
158+
ptr: NonNull<Opaque>,
158159
}
159160

160161
use std::ffi::FromBytesWithNulError;
@@ -215,12 +216,10 @@ impl ArcCStr {
215216
let sz = aus + buf.len() + 1;
216217
let aul = alloc::allocator::Layout::from_size_align(sz, aual).unwrap();
217218

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);
224223
// initialize the AtomicUsize to 1
225224
{
226225
let atom: &mut atomic::AtomicUsize = mem::transmute(s.as_mut());
@@ -231,9 +230,7 @@ impl ArcCStr {
231230
// add \0 terminator
232231
*cstr.offset(buf.len() as isize) = 0u8;
233232
// and we're all good
234-
ArcCStr {
235-
ptr: NonNull::new(s.as_ptr().offset(0)).unwrap(),
236-
}
233+
ArcCStr { ptr: s }
237234
}
238235

239236
/// Gets the number of pointers to this string.
@@ -283,7 +280,7 @@ impl ArcCStr {
283280
size_of::<atomic::AtomicUsize>() + blen,
284281
align_of::<atomic::AtomicUsize>(),
285282
).unwrap();
286-
alloc::heap::Heap.dealloc(self.ptr.as_ptr().offset(0) as *mut _, aul)
283+
alloc::alloc::Global.dealloc(self.ptr, aul)
287284
}
288285

289286
#[inline]
@@ -305,7 +302,7 @@ impl ArcCStr {
305302
/// assert!(!ArcCStr::ptr_eq(&five, &other_five));
306303
/// ```
307304
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
309306
}
310307
}
311308

@@ -375,7 +372,7 @@ impl Deref for ArcCStr {
375372
// place.
376373
//
377374
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))) }
379376
}
380377
}
381378

@@ -615,7 +612,9 @@ impl serde::Serialize for ArcCStr {
615612
// once to find the length, then once more to serialize...
616613
let aus = size_of::<atomic::AtomicUsize>();
617614
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+
};
619618
serializer.serialize_bytes(bytes)
620619
}
621620
}

0 commit comments

Comments
 (0)