Looking at the documentation for the trait std::convert::From, it says:
Used to do value-to-value conversions while consuming the input value.
To me, and looking at the fn from(T) -> Self signature, it sounds like the function takes ownership of the argument.
How can e.g. String implement the From<&str> trait? It cannot take ownership from the shared reference.
I want to implement a "viewer" structure; something that doesn't take ownership: Say a &[u8] represents my binary data; the first byte is my "data id".
struct DataView {
id: u8,
data: &[u8],
}
Then I would like to be able to do something like:
impl From<&[u8]> for DataView {
fn from(buffer: &[u8]) -> DataView {
DataView {
id: buffer[0],
data: &buffer[1..],
}
}
}
Is something like this possible? Does such a structure make sense if I am only interested in "viewing"?