Hi, newbie here. Just started learning rust and now trying the Rocket library.
The question is inspired with a Rocket example of paste bin implementation.
We have an ID of a paste, like this
pub struct PasteID<'a>(Cow<'a,str>);
Example shows how to make a path with it
fn upload(id: PasteID) ... {
...
let p = format!("/upload/{id}", id=id)
...Path::new(p)...
...
}
I don’t actually like that and tried this instead:
...
let p = Path::new("/dir").join(id)
...
But rust complains (playground https://goo.gl/1irdH5)
error[E0277]: the trait bound `PasteID<'_>: std::convert::AsRef<std::path::Path>` is not satisfied
--> src/main.rs:8:31
|
8 | let p = Path::new("/dir").join(id);
| ^^^^ the trait `std::convert::AsRef<std::path::Path>` is not implemented for `PasteID<'_>`
What should I actually do? Really implement AsRef
for PasteID
?
Isn’t it already implemented for Cow
? If it is really needed, then how to do that correctly?
It does actually work if I cast PasteID to_string explcitely:
let p = Path::new("/dir").join(id.to_string())
but that seems weird.
So what is an idiomatic way?