Skip to content

Implement Deref for Box #18624

Closed
Closed
@kennytm

Description

@kennytm

As titled. Currently Box does not implement Deref:

fn f<G: Deref<int>>(g: &G) -> int { **g }
fn main() {
    f(&std::rc::Rc::new(1i));   // ok
    f(&box 1i); // error. Deref<int> not impl for Box<int>
}

But Box should be able to implement Deref.

impl<Sized? T> Deref<T> for Box<T> {
    fn deref(&self) -> &T { &**self }  // or any equivalent like `self.0.as_ref().unwrap()`
}
impl<Sized? T> DerefMut<T> for Box<T> {
    fn deref_mut(&mut self) -> &mut T { &mut **self }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions