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
As there are several proposals on how to modify the Alloc trait, I tried to combine most of the proposed changes. With this issue I would like to try to create a basis for what the new Alloc trait might look like. It might be easier to continue working with it to test new features.
Separate dealloc from Alloc into other trait #9: Also separate realloc into a ReallocRef trait as implementing an allocator will be the unusual case, but grants the user more possibilities to use it.
For simplicity I skipped the _excess and _in_place API as it does not change.
Starting with the BuildAlloc trait:
// `Sized` is required for `AllocRef`pubtraitBuildAlloc:Sized{typeAllocRef:AllocRef<BuildAlloc = Self>;unsafefnbuild_alloc(&mutself,ptr:NonNull<u8>,layout:Layout) -> Self::AllocRef;}
The Alloc trait got separated into AllocRef, ReallocRef, and DeallocRef:
pubtraitDeallocRef:Sized{unsafefndealloc(&mutself,ptr:NonNull<u8>,layout:Layout);}pubtraitReallocRef{typeDeallocRef:DeallocRef;typeError;unsafefnrealloc(&mutself,ptr:NonNull<u8>,layout:Layout,new_size:usize,) -> Result<NonNull<u8>,Self::Error>;}pubtraitAllocRef:DeallocRef{typeBuildAlloc:BuildAlloc<AllocRef = Self>;typeReallocRef:ReallocRef<DeallocRef = Self>;// Not necessarily the same Error as in `ReallocError`typeError;// Note that `alloc` and `alloc_zeroed` does not need to be unsafe anymore as zero// sized layouts are not allowed. This requires something like `NonZeroLayout` to // hold thisfnalloc(&mutself,layout:Layout) -> Result<NonNull<u8>,Self::Error>;#[allow(unused_variables)]fnalloc_zeroed(&mutself,layout:Layout) -> Result<NonNull<u8>,Self::Error>{unimplemented!()}fnget_build_alloc(&mutself) -> Self::BuildAlloc;}
I wrote a small sealed trait for determine infallible allocation:
mod private {pubtraitInfallible{}implInfalliblefor ! {}implInfalliblefor core::convert::Infallible{}}
For Box I chose not to use Box<T, D: BuildDealloc> for three reasons:
calling Box::new_in could not deduce D as new_in takes AllocRef
AllocRef can directly be stored as DeallocRef
BuildAlloc can be created by AllocRef, BuildDealloc (if exists) can be created from DeallocRef
pubstructBox<T: ?Sized,D:DeallocRef>(Unique<T>,D);impl<T,A:AllocRef>Box<T,A>{// Thanks to the associated error type, we can add two methods, a fallible and // an infallible one.// To maintain backwards compatibility, Box could default `A` to something like// `AbortAlloc<Global>` which aborts on failure thus enabling `new_in` by default.pubfnnew_in(x:T,a:A) -> SelfwhereA::Error: private::Infallible,{ifletOk(b) = Self::try_new_in(x, a){
b
}else{unreachable!("Infallible allocation failed")}}pubfntry_new_in(x:T,muta:A) -> Result<Self,A::Error>{let layout = Layout::new::<T>();letmut ptr = a.alloc(layout)?.cast();unsafe{*ptr.as_mut() = x;}Ok(Self(ptr.into(), a))}}
Actually I missed some details of #12 in conjunction with #9.
As theocrafting won't help very much here, I'm closing this for now and will post a new Issue in a few days with another attempt.
As there are several proposals on how to modify the
Alloc
trait, I tried to combine most of the proposed changes. With this issue I would like to try to create a basis for what the newAlloc
trait might look like. It might be easier to continue working with it to test new features.Issues, I took into account:
Alloc
toAllocRef
#8: I chose to renameAlloc
toAllocRef
, no strong opinion here.dealloc
fromAlloc
into other trait #9: Also separaterealloc
into aReallocRef
trait as implementing an allocator will be the unusual case, but grants the user more possibilities to use it.Box<T, A: BuildAlloc>
#12std::alloc::Layout
and did not implement something like aLayoutBuilder
orNonZeroLayout
.Alloc::{alloc,dealloc}_array
andAlloc::{alloc,dealloc}_one
APIs #18: I think it's better to leave them out in the first iteration to be conservative. It can always be added later.For simplicity I skipped the
_excess
and_in_place
API as it does not change.Starting with the
BuildAlloc
trait:The
Alloc
trait got separated intoAllocRef
,ReallocRef
, andDeallocRef
:I wrote a small sealed trait for determine infallible allocation:
For
Box
I chose not to useBox<T, D: BuildDealloc>
for three reasons:Box::new_in
could not deduceD
asnew_in
takesAllocRef
AllocRef
can directly be stored asDeallocRef
BuildAlloc
can be created byAllocRef
,BuildDealloc
(if exists) can be created fromDeallocRef
I also put this in a playground.
The text was updated successfully, but these errors were encountered: