CAD97 / CAD97/pointer-utils

`slice-dst`: Provide example for custom 'slices' using `SliceWithHeader`

Aperta
#69 7 commenti 1 reazione 0 assegnatari Vedi su GitHub
documentation good first issue
Lingua principale
Rust
Stelle
137
Fork
15
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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:

```rust
struct MyString(Box>);
```

To mimicry `str` I want to implement `Borrow for MyString` and `ToOwned for MyStr` (`type Owned = MyString`). However, I have a problem defining `MyStr`. I could use a type alias:

```rust
type MyStr = SliceWithHeader;
```

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`:

```rust
struct MyStr(SliceWithHeader);

impl Borrow 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?

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.