Can I borrow a slice from a custom type?

Viewed 557

It's possible to borrow a Vec<u32> into either a &Vec<u32> or a &[u32]. I thought this was thanks to either the AsRef or Borrow traits. However, I was unable to implement such borrowing on my own custom type. Am I barking up the wrong tree here?

use std::borrow::Borrow;

struct MyArray([u32; 5]);

impl MyArray {
    fn new() -> MyArray {
        MyArray([42; 5])
    }
}

impl AsRef<[u32]> for MyArray {
    fn as_ref(&self) -> &[u32] {
        &self.0
    }
}

impl Borrow<[u32]> for MyArray {
    fn borrow(&self) -> &[u32] {
        &self.0
    }
}

fn main() {
    let ma = MyArray::new();
    let _: &[u32] = &ma; // compilation failure
}
1 Answers

You are looking for std::ops::Deref:

In addition to being used for explicit dereferencing operations with the (unary) * operator in immutable contexts, Deref is also used implicitly by the compiler in many circumstances. This mechanism is called 'Deref coercion'. In mutable contexts, DerefMut is used.

Modified code:

use std::ops::Deref;

struct MyArray([u32; 5]);

impl MyArray {
    fn new() -> MyArray {
        MyArray([42; 5])
    }
}

impl Deref for MyArray {
    type Target = [u32];

    fn deref(&self) -> &[u32] {
        &self.0
    }
}

fn main() {
    let ma = MyArray::new();
    let _: &[u32] = &ma;
}
Related