CAD97 / CAD97/pointer-utils

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

Open
#69 7 comments 1 reaction 0 assignees View on GitHub
documentation good first issue
Dominant language
Rust
Stars
137
Forks
15
PR merge metrics
No merged PRs in 30d

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?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.