Skip to content

Additional traits for allocator aware types #15

Open
@TimDiekmann

Description

@TimDiekmann

When adding allocators, many current traits become useless. Those traits should be either extended to support allocators or orthogonal traits should be added.

  • Default: Add DefaultIn:

    pub trait DefaultIn {
        type Alloc: Alloc;
    
        fn default_in(allocator: Self::Alloc) -> Self;
    }
  • FromIterator: Add FromIteratorIn:

    pub trait FromIteratorIn<T, A: Alloc>: Sized {
        fn from_iter_in<I: IntoIterator<Item = T>>(iter: I, allocator: A) -> Self;
    }
  • Iterator: Add collect_in:

    pub trait Iterator {
        // ...
    
        #[inline]
        #[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"]
        fn collect_in<T: FromIteratorIn<Self::Item, A>, A: Alloc>(self, allocator: A) -> T {
            FromIteratorIn::from_iter_in(self, allocator)
        }
    }
  • (Try)From and (Try)Into: Add (Try)FromIn and (Try)IntoIn :

    pub trait FromIn<T> {
        type Alloc: Alloc;
    
        fn from_in(t: T, allocator: Self::Alloc) -> Self;
    }
    
    pub trait IntoIn<T> {
        type Alloc: Alloc;
    
        fn into_in(self, allocator: Self::Alloc) -> T;
    }
    
    
    impl<T, U: FromIn<T>> IntoIn<U> for T {
        type Alloc = U::Alloc;
    
        fn into_in(self, allocator: Self::Alloc) -> U {
            U::from_in(self, allocator)
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-TraitsOther traits beside the Alloc traitProposal

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions