Skip to content

autoderef: DerefMut doesn't work with Box #26205

Closed
@japaric

Description

@japaric

STR

use std::ops::{Deref, DerefMut};

struct Foo;

impl Foo {
    fn foo(&self) {}
    fn foo_mut(&mut self) {}
}

struct Bar(Foo);

impl Deref for Bar {
    type Target = Foo;

    fn deref(&self) -> &Foo {
        &self.0
    }
}

impl DerefMut for Bar {
    fn deref_mut(&mut self) -> &mut Foo {
        &mut self.0
    }
}

#[cfg(not(works))]
fn test(mut bar: Box<Bar>) {
    bar.foo();  // OK
    bar.foo_mut();
    //~^ error: cannot borrow immutable borrowed content as mutable (WRONG)
}

#[cfg(works)]
fn test(mut bar: Box<Bar>) {
    bar.deref_mut().foo_mut();
    //~^ but this actually works!
}

fn main() {}

Version

rustc 1.2.0-nightly (613e57b44 2015-06-01) (built 2015-06-02)

cc @nikomatsakis @eddyb

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions