-
Notifications
You must be signed in to change notification settings - Fork 9
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
Additional traits for allocator aware types #15
Comments
What would various intermediate iterator ops that might need to allocate do? e.g. |
This is not related to |
Should they really? Rather than systematically adding new allocator-aware APIs for everything in the standard library that might allocate, I’d prefer to judge individual use cases. For example, what actual generic code would need to have conversion traits for its type parameters, and need to be allocator-aware? For APIs we do want to add, why should the allocator type ever be an associated type chosen by the |
Good point, a generic parameter would match better I guess. |
I am not sure what problem each of the proposed traits actually solves. Maybe you could elaborate on that? |
It's the same use case as before: if I want to create a |
If |
With this argument we could also deprecate |
|
In #7 the discussion came up to also provide I liked the proposal in #7 (comment) and tested around a bit with this at the pub trait CloneIn<A: AllocRef>: Sized {
type Cloned;
fn clone_in(&self, a: A) -> Self::Cloned
where
A: AllocRef<Error = !>;
fn try_clone_in(&self, a: A) -> Result<Self::Cloned, A::Error>;
} Adding this to #[allow(clippy::use_self)]
impl<T: Clone, A: AllocRef, B: BuildAllocRef> CloneIn<A> for Box<T, B>
where
B::Ref: AllocRef,
{
type Cloned = Box<T, A::BuildAlloc>;
fn clone_in(&self, a: A) -> Self::Cloned
where
A: AllocRef<Error = !>,
{
Box::new_in(self.as_ref().clone(), a)
}
fn try_clone_in(&self, a: A) -> Result<Self::Cloned, A::Error> {
Box::try_new_in(self.as_ref().clone(), a)
}
} |
The above proposed |
Do you mean it doesn't work well when implementing |
I mean implementing |
I think this is fine, as |
Has anyone attempted this yet? From the discussions it looks like these are the traits we are interested in:
pub trait DefaultIn<A: Allocator> {
fn default_in(allocator: A) -> Self;
}
pub trait FromIteratorIn<T, A: Allocator>: Sized {
fn from_iter_in<I: IntoIterator<Item = T>>(iter: I, allocator: A) -> Self;
}
pub trait Iterator {
// ...
#[inline]
fn collect_in<T: FromIteratorIn<Self::Item, A>, A: Allocator>(self, allocator: A) -> T {
FromIteratorIn::from_iter_in(self, allocator)
}
}
pub trait FromIn<T, A: Allocator> {
fn from_in(t: T, allocator: A) -> Self;
}
pub trait IntoIn<T, A: Allocator> {
fn into_in(self, allocator: A) -> T;
}
...
impl<T, U: FromIn<T, A>, A: Allocator> IntoIn<U, A> for T {
fn into_in(self, allocator: A) -> U {
U::from_in(self, allocator)
}
} Just spitballing, but would it be possible to add generic |
I think it'd be way better if we add generic methods to existing traits because it reduces API surface. |
The challenge is you want something like:
But for backwards compatibility, a default implementation of Providing allocators via context seems to be a much better idea to me. |
Is there even an RFC for Contexts? That seems like it was just an idea (a really good one IMHO). I don’t think allocators can wait on that. |
Given they're not stable, they can wait indefinitely. Contexts are very good idea, which I believe is the first thing add to the type system after const generics. It would be nice to have them before stable alloc |
When adding allocators, many current traits become useless. Those traits should be either extended to support allocators or orthogonal traits should be added.
Default
: AddDefaultIn
:FromIterator
: AddFromIteratorIn
:Iterator
: Addcollect_in
:(Try)From
and(Try)Into
: Add(Try)FromIn
and(Try)IntoIn
:The text was updated successfully, but these errors were encountered: