Skip to content

provides a `MaybeOwned<'a,T>` type different to std's `Cow` it implements `From<T>` and `From<&'a T>` and does not require `ToOwned`

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

rustonaut/maybe-owned

Repository files navigation

maybe-owned Crates.io maybe-owned maintenance License License

provides a MaybeOwned<'a,T> type different to std's Cow it implements From<T> and From<&'a T> and does not require ToOwned


This crate provides a MaybeOwned<'a,T> enum. Different to std::borrow::Cow it implements From<T> and From<&'a T> and does not require a ToOwned implementation. While this can be nice for API's mainly consuming T's not implementing ToOwned or implementing ToOwned through Clone it also means it's borrowed version of String is &String and not &str making it less performant for cases like String or Vec.

Documentation can be viewed on docs.rs.

Example

Take a look at the examples dir and the documentation for more complete examples.

The main benefit of MaybeOwned over Cow is for API design, allowing API consumer to pass in both T and &'a T:

//... in a trait implementation
    fn register<D>(&mut self, key: SomeId, data: D)
        where D: Into<MaybeOwned<'a, Data>>
    {
        self.map.entry(key).or_insert_with(||data.into());
    }
//...

//... in usage
    // use owned data
    registry.register(id1, data_owned);
    // use a reference to the data
    registry.register(id2, &data_ref);
    // it ok to use the same reference again
    registry.register(id3, &data_ref);
//...

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Contributors: ./CONTRIBUTORS.md

Change Log

See ./CHANGELOG.md

About

provides a `MaybeOwned<'a,T>` type different to std's `Cow` it implements `From<T>` and `From<&'a T>` and does not require `ToOwned`

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published

Languages