Closed
Description
Given the following code:
use std::ffi::{OsStr, OsString};
use std::path::Path;
fn check(p: &dyn AsRef<Path>) {
let m = std::fs::metadata(&p);
println!("{:?}", &m);
}
fn main() {
let s: OsString = ".".into();
let s: &OsStr = &s;
check(s);
}
The current output is:
error[E0277]: the size for values of type `[u8]` cannot be known at compilation time
--> src/main.rs:12:11
|
12 | check(s);
| ----- ^ doesn't have a size known at compile-time
| |
| required by a bound introduced by this call
|
= help: within `OsStr`, the trait `Sized` is not implemented for `[u8]`
= note: required because it appears within the type `OsStr`
= note: required for the cast to the object type `dyn AsRef<Path>`
Ideally the output would suggest borrowing, since this:
check(&s);
compiles and works just fine.
The problem is that you can't make an object which is a wide pointer directly into a trait object refernce, because it would have to be a triple-sized pointer. But often, you can just reborrow instead.