borrowme

github crates.io docs.rs build status

The missing compositional borrowing for Rust.

Rust comes with two sibling traits which that can convert from owned to borrowed and vice versa: ToOwned and Borrow.

These can convert most simple types such as &str to and from String. But lets think of this in a broader perspective. How to we convert a type that has lifetimes, to one which does not? This crate defines its own [ToOwned] and [Borrow] traits which serve a similar purpose to the ones in std but are implemented so that they can do this generically.

To help us implement these traits the #[borrowme] attribute macro is provided (see this section for why it's not a derive macro).

```rust

[borrowme]

[derive(Debug, Clone)]

struct Word<'a> { text: &'a str, lang: Option<&'a str>, examples: Vec<&'a str>, }

```

With this we get the following additional structs and trait implementations:

```rust

[derive(Debug, Clone)]

struct OwnedWord { text: String, lang: Option, examples: Vec, }

impl borrowme::ToOwned for Word<'_> { type Owned = OwnedWord;

fn to_owned(&self) -> OwnedWord {
    /* .. */
}

}

impl borrowme::Borrow for OwnedWord { type Target<'a> = Word<'a>;

fn borrow(&self) -> Word<'_> {
    /* .. */
}

} ```