Skip to content
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

Higher-Ranked types over lifetime in trait associated types #1965

amosonn opened this issue Apr 4, 2017 · 2 comments


None yet
3 participants
Copy link

commented Apr 4, 2017

Currently the usability of trait associated types (as return values of methods) is mostly limited to those which have no lifetime parameters. That's because you need to fix those lifetime parameters at the impl level, whereas many times you would want a lifetime parameter that's dependant on the lifetimes of method arguments (say &'a self).

Example 1:

trait Position { ... }

struct MyStructPosition<'a> {
    _ref: &'a MyStruct,
    pos: u64,

impl<'a> Position for MyStructPosition<'a> { ... }

trait Positionable {
    type PositionType: Position;
    fn<'a>(&'a self, pos: u64) -> Self::PositionType<'a>;

impl Positionable for MyStruct {
    type PositionType = MyStructPosition;
    fn<'a>(&'a self, pos: u64) -> MyStructPosition<'a> { ... }

Example 2:

struct RaiiAndOption<T> {
    guard: RaiiGuard,
    data: Option<const * T>,

impl<T> Deref for RaiiAndOption<T> {
    type Target = Option<&T>;
    fn deref(&'a self) -> &'a Option<&'a T> {
        &|x| unsafe { mem::transmute<const * T, &'a T>(x) })

It is true that the second example uses transmute, but this is safe - our inner references can only live as long as we can borrow RaiiAndOption, which means no later than the RaiiGuard is dropped.

Of course, the first one is reasonable syntax; the second is more of a stretch, verbatim, but even if the syntax will require a type OptionRef<'a, T> = Option<&'a T> that's still good.


This comment has been minimized.

Copy link

commented Apr 4, 2017

#1598 is an existing RFC for "Associated type constructors". Is this the same?


This comment has been minimized.

Copy link

commented Apr 4, 2017

Yes. Thanks!

@amosonn amosonn closed this Apr 4, 2017

@Centril Centril added the T-lang label Feb 23, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.