You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add an Allocator impl for &mut A when A: ?Sized + Allocator that forwards to the underlying implementation
Problem statement
&mut A currently doesn't impl Allocator when A does
Motivating examples or use cases
the current state of things makes it cumbersome to work with Send allocators (especially type erased dyn 'a + Send + Allocator). examples of allocators that are Send, but not Sync are local allocators that wrap a RefCell to satisfy the immutable ref api.
Solution sketch
we can just copy the &A implementation
unsafeimpl<A>Allocatorfor&mutAwhereA:Allocator + ?Sized,{#[inline]fnallocate(&self,layout:Layout) -> Result<NonNull<[u8]>,AllocError>{(**self).allocate(layout)}#[inline]fnallocate_zeroed(&self,layout:Layout) -> Result<NonNull<[u8]>,AllocError>{(**self).allocate_zeroed(layout)}#[inline]unsafefndeallocate(&self,ptr:NonNull<u8>,layout:Layout){// SAFETY: the safety contract must be upheld by the callerunsafe{(**self).deallocate(ptr, layout)}}#[inline]unsafefngrow(&self,ptr:NonNull<u8>,old_layout:Layout,new_layout:Layout,) -> Result<NonNull<[u8]>,AllocError>{// SAFETY: the safety contract must be upheld by the callerunsafe{(**self).grow(ptr, old_layout, new_layout)}}#[inline]unsafefngrow_zeroed(&self,ptr:NonNull<u8>,old_layout:Layout,new_layout:Layout,) -> Result<NonNull<[u8]>,AllocError>{// SAFETY: the safety contract must be upheld by the callerunsafe{(**self).grow_zeroed(ptr, old_layout, new_layout)}}#[inline]unsafefnshrink(&self,ptr:NonNull<u8>,old_layout:Layout,new_layout:Layout,) -> Result<NonNull<[u8]>,AllocError>{// SAFETY: the safety contract must be upheld by the callerunsafe{(**self).shrink(ptr, old_layout, new_layout)}}}
Alternatives
an alternative solution is using a ByMut<A> wrapper that's a thin wrapper over &mut A, but this is an unnecessary hoop to make users jump through
The text was updated successfully, but these errors were encountered:
Proposal
add an
Allocator
impl for&mut A
whenA: ?Sized + Allocator
that forwards to the underlying implementationProblem statement
&mut A
currently doesn't implAllocator
whenA
doesMotivating examples or use cases
the current state of things makes it cumbersome to work with Send allocators (especially type erased
dyn 'a + Send + Allocator
). examples of allocators that are Send, but not Sync are local allocators that wrap aRefCell
to satisfy the immutable ref api.Solution sketch
we can just copy the
&A
implementationAlternatives
an alternative solution is using a
ByMut<A>
wrapper that's a thin wrapper over&mut A
, but this is an unnecessary hoop to make users jump throughThe text was updated successfully, but these errors were encountered: