Skip to content

Move SliceIndex type parameter to parameters #3483

Open

Description

If I have a struct like this:

struct T1 {}
struct T2 {}

pub struct Aggregate {
    v1: Vec<T1>,
    v2: Vec<T2>,
}

impl Aggregate {
    pub fn compute<R>(&self, range: R) -> Option<u32> {
        for v in self.v1.get(range)? {
            output += v.compute();
        }
        for v in self.v2.get(range)? {
            output += v.compute();
        }
        Some(output)
    }
}

The type bound for R would need to be R: SliceIndex<[T1]> + SliceIndex<[T2]>, which exposes internal implementation details.

I propose moving the <T> in SliceIndex<T> to the methods and leverage GATs:

trait SliceIndex : Sized {
    type Output<T: ?Sized>: ?Sized;
    fn get<T: ?Sized>(self, slice: &T) -> Option<&Self::Output<T>>;
    fn get_mut<T: ?Sized>(self, slice: &mut T) -> Option<&mut Self::Output<T>>;
    // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions