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
}