Open
Description
Thank you for this awesome crate. I always respect when people dive into the unsafe
world of Rust.
I'd like to use the slice-dst
crate to build something similar to String
and str
. Let's assume:
struct MyString(Box<SliceWithHeader<Info, u8>>);
To mimicry str
I want to implement Borrow<MyStr> for MyString
and ToOwned for MyStr
(type Owned = MyString
). However, I have a problem defining MyStr
. I could use a type alias:
type MyStr = SliceWithHeader<Info, u8>;
But this is inconvenient and won't allow some custom impl MyStr
. What I would like is to use is a new-type-pattern. But this makes it difficult implementing Borrow
:
struct MyStr(SliceWithHeader<Info, u8>);
impl Borrow<MyStr> for MyString {
fn borrow(&self) -> &MyStr {
&*self.0 // <-- requires some cast/convertion/idk
}
}
Would it be safe to simple cast the reference from the Box
within MyString
to a &MyStr
or would this be undefined behavior?