Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SmallBox covariant over T #40

Merged
merged 1 commit into from
Dec 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/smallbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ macro_rules! smallbox {
/// An optimized box that store value on stack or on heap depending on its size
pub struct SmallBox<T: ?Sized, Space> {
space: MaybeUninit<UnsafeCell<Space>>,
ptr: *mut T,
ptr: *const T,
_phantom: PhantomData<T>,
}

Expand Down Expand Up @@ -229,7 +229,7 @@ impl<T: ?Sized, Space> SmallBox<T, Space> {
#[inline]
unsafe fn as_mut_ptr(&mut self) -> *mut T {
if self.is_heap() {
self.ptr
self.ptr as *mut T
} else {
sptr::with_metadata_of_mut(self.space.as_mut_ptr(), self.ptr)
}
Expand Down Expand Up @@ -260,7 +260,7 @@ impl<T: ?Sized, Space> SmallBox<T, Space> {
if this.is_heap() {
let layout = Layout::new::<T>();
unsafe {
alloc::dealloc(this.ptr.cast(), layout);
alloc::dealloc(this.ptr as *const u8 as *mut u8, layout);
}
}

Expand Down Expand Up @@ -364,7 +364,7 @@ impl<T: ?Sized, Space> ops::Drop for SmallBox<T, Space> {
let layout = Layout::for_value::<T>(&*self);
ptr::drop_in_place::<T>(&mut **self);
if self.is_heap() {
alloc::dealloc(self.ptr.cast(), layout);
alloc::dealloc(self.ptr as *const u8 as *mut u8, layout);
}
}
}
Expand Down Expand Up @@ -665,4 +665,12 @@ mod tests {
cellbox.set(1);
assert_eq!(cellbox.get(), 1);
}

#[test]
fn test_variance() {
#[allow(dead_code)]
fn test<'short, 'long: 'short>(val: SmallBox<&'long str, S1>) -> SmallBox<&'short str, S1> {
val
}
}
}
Loading