`slice-dst`: Provide example for custom 'slices' using `SliceWithHeader`
- Langage dominant
- Rust
- Étoiles
- 137
- Forks
- 15
- Métriques de merge des PR
- Aucune PR mergée en 30 j
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:
```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?
Guide de contribution
Aucun guide de contribution indexé pour ce dépôt
Piste de recherche
Commencez par le type SliceWithHeader et l’exemple personnalisé MyString/MyStr de l’issue ; aucun fichier ni test n’est identifié. Établissez les exigences de sécurité pour la conversion de référence proposée, puis documentez un exemple complet de slice personnalisé ainsi que son comportement attendu pour Borrow/ToOwned.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- rust
- Domaine
- documentation
- Type d'issue
- Documentation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100