timothee-haudebourg / cc-traits

Apache License 2.0
11 stars 8 forks source link

Associated Reference Types #1

Closed danii closed 3 years ago

danii commented 3 years ago

I have added different associated types for the reference types returned from trait methods such as Get::get, using generic associated types to enable patterns such as collections that return guarded references. I've done this in anticipation of Rust stabilizing GATs soon, as their blog post has led me to believe. To further emphasize the pattern of collections that are locks, I've added Lock traits which provide the same interface as their non-Lock counterparts, only behind an immutable reference rather than a mutable one. To showcase these new features I've also added implementations for the traits on structures in the popular dashmap crate.

I'll ready this pull request if and when (hopefully just a matter of when) GATs are stabilized.

timothee-haudebourg commented 3 years ago

Thinking about your proposition, I think I would prefer to break the Collection trait into smaller parts to preserve the spirit of this crate (small, composable traits). Not all collections can provide references to its items, and even less collections are mutable. So in many case, providing ItemRef and ItemMut will not be possible. I propose to split your Collection definition into smaller traits:

pub trait Collection {
  type Item;
}

pub trait CollectionRef: Collection {
  type ItemRef<'a>: ...;
}

pub trait CollectionMut: Collection {
  type ItemMut<'a>: ...;
}
danii commented 3 years ago

Agh I just pushed broken code because I didn't put the Self: 'a bounds on Output in the implementations! :scream: Don't review just yet

danii commented 3 years ago

Thinking about your proposition, I think I would prefer to break the Collection trait into smaller parts to preserve the spirit of this crate (small, composable traits). Not all collections can provide references to its items, and even less collections are mutable. So in many case, providing ItemRef and ItemMut will not be possible. I propose to split your Collection definition into smaller traits:

pub trait Collection {
  type Item;
}

pub trait CollectionRef {
  type ItemRef<'a>: ...;
}

pub trait CollectionMut {
  type ItemMut<'a>: ...;
}

That is probably for the better, I'll add that too.

timothee-haudebourg commented 3 years ago

@danii can you fix the Slab implementation so I can merge? I'm actually going to need these changes very soon.

danii commented 3 years ago

@danii can you fix the Slab implementation so I can merge? I'm actually going to need these changes very soon.

Okay, done, sorry I keep missing some things. 😅 If you'd like to merge it now, I'd recommend to merge it into a different branch until GATs are stabilized.

timothee-haudebourg commented 3 years ago

@danii FIY I think I'm going to drop the lifetime parameter for the Output types as we don't seem to have a use case for it and it messes up the trait aliases where I use an equality bound Output=bool. As far as I know, equality bounds are not supported for GATs yet.