#Generic implementation:

5 messages · Page 1 of 1 (latest)

light trail
#

Essentially:
I have a generic implementation of a for a trait that takes a PrimaryID by reference from an object.
What im struggling with is taking that id by reference from an iterable of that object

    type PrimaryId: Serialize + DeserializeOwned + Hash + Eq + Display + Clone + Send + Sync + 'static;
    fn get_primary_id(&self) ->Self::PrimaryId;
}


pub trait ExtractPrimaryIds<T: GetPrimaryId> {
    fn primary_ids(&self) -> Vec<&T::PrimaryId>;
}

impl<'a, T, I> ExtractPrimaryIds<T> for &'a I
where
    T: GetPrimaryId<PrimaryId = &'a <T as GetPrimaryId>::PrimaryId>  + 'a,
    &'a I: IntoIterator<Item = &'a T>,
{
    fn primary_ids(&self) -> Vec<&'a T::PrimaryId> {
        self.into_iter()
            .map(|item| item.get_primary_id())
            .collect_vec()
    }
}```
naive plinth
#

references don't implement DeserializeOwned, so there can be no type that satisfies GetPrimaryId<PrimaryId = &(anything)> because there can be no &(anything) that satisfies the requirements on PrimaryId

#

if you want to sidestep basically all of the lifetime issues, have primary_ids return a Vec<T::PrimaryId> (no reference). if you actually need a Vec<&T::PrimaryId>, consider having get_primary_id return &Self::PrimaryId instead

light trail
#

my bad. GetPrimaryId was supposed to return a reference but i have missed it and making a mess of it

#

I've been scratching my head on this one to long