On Mon, Feb 3, 2014 at 11:33 AM, Niko Matsakis <n...@alum.mit.edu> wrote:

> On Sat, Feb 01, 2014 at 03:42:45PM -0800, Vadim wrote:
> > Since &'a Foo<T> currently means "the return value is a reference into
> > something that has lifetime 'a",  'a Foo<T> feels like a natural
> extension
> > for saying "the return value is a reference-like thing whose safety
> depends
> > on something that has lifetime 'a still being around".
> > Foo<'a,T>, of the other hand... it is not obvious to me why would it
> > necessarily mean that.
>
> It does not, in fact, *necessarily* mean that, though certainly it
> most commonly does. It will depend on the definition of the `Foo` type
> and how the lifetime parameter is used within the type, as you say. It
> seems then that you did not mean for `'a Foo<T>` to be syntactic sugar
> for `Foo<'a, T>` but rather a new kind of type: kind of a "by value
> that is limited to 'a".
>
> > I've been around Rust for almost a year now,  and certainly since the
> time
> > the current lifetime notation has been introduced, and I *still *could
> not
> > explain to somebody, why a lifetime parameter appearing among the type
> > parameters of a trait or a struct refers to the lifetime of that trait or
> > struct.
>
> As I wrote above, a lifetime parameter does not by itself have any
> effect, just like a type parameter. Both type and lifetime parameters
> are simply substituted into the struct body, and any limitations arise
> from there. That is, if I have
>
>     struct Foo<'a> {
>         x: &'a int
>     }
>
> then `Foo<'xyz>` is limited to the lifetime `'xyz` because `Foo`
> contains a field `x` whose type is (after substitution) `&'xyz int`,
> and that *field* cannot escape the lifetime `'xyz`.
>
> Thus, there are in fact corner cases where the lifetime parameter has
> no effect, and so it is not the case that `SomeType<'xyz>` is
> necessarily limited to `'xyz` (the most obvious being when 'xyz is
> unused within the struct body, as you suggest).
>
>
How does this apply to traits?    If I look at "trait MutableVector<'a, T>"
there's nothing that would connect 'a and self.   Only when we get down to
"impl<'a,T> MutableVector<'a, T> for &'a mut [T]" can you see that 'a is
the lifetime of the type it's being implemented for.   Which I guess if
fine for static dispatch.     But what about dynamic dispatch?   Is the
following supposed to work?:

fn bar(x: &mut MutableVector<int>) {
    let y = x.mut_iter();
    ...
}

Vadim
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to