Skip to content

It is not possible to call methods with explicit litefimes on self when implementing a trait #15073

@arjantop

Description

@arjantop

This used to work:

struct Foo<'a> {
    bar: &'a mut int
}

impl<'a> Foo<'a> {
    fn get(&'a mut self) -> &'a mut int {
        return &mut *self.bar
    }

    fn baz(&mut self) {
        let a = self.get();
    }
}

Now explicit lifetime on self is required if calling get:

    fn baz(&'a mut self) {
        let a = self.get();
    }

This means that you can't call methods with explicit lifetimes on self when implementing a trait:

struct Foo<'a> {
    bar: &'a mut int
}

trait Trait {
    fn baz(&mut self);
}

impl<'a> Foo<'a> {
    fn get(&'a mut self) -> &'a mut int {
        return &mut *self.bar
    }
}

impl<'a> Trait for Foo<'a> {
    fn baz(&mut self) {
        let a = self.get();
    }
}

The real usecase is thid:

fn get_writer(&'a mut self) -> &'a mut io::Writer {
    if self.writers.len() == 0 {
        &mut self.writer as &'a mut io::Writer
    } else {
        self.writers.mut_last().unwrap() as &'a mut io::Writer
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions