https://github.com/rustonaut/maybe-owned
provides a `MaybeOwned<'a,T>` type different to std's `Cow` it implements `From<T>` and `From<&'a T>` and does not require `ToOwned`
https://github.com/rustonaut/maybe-owned
Last synced: 3 months ago
JSON representation
provides a `MaybeOwned<'a,T>` type different to std's `Cow` it implements `From<T>` and `From<&'a T>` and does not require `ToOwned`
- Host: GitHub
- URL: https://github.com/rustonaut/maybe-owned
- Owner: rustonaut
- License: apache-2.0
- Created: 2017-05-17T10:38:20.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2023-03-05T18:28:17.000Z (over 3 years ago)
- Last Synced: 2025-08-28T17:10:15.420Z (10 months ago)
- Language: Rust
- Size: 69.3 KB
- Stars: 31
- Watchers: 1
- Forks: 5
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# maybe-owned [](https://crates.io/crates/maybe-owned) [](https://docs.rs/maybe-owned) [](https://img.shields.io/badge/maintenance-passively--maintained-blue.svg) [](https://opensource.org/licenses/MIT) [](https://opensource.org/licenses/Apache-2.0)
**provides a `MaybeOwned<'a,T>` type different to std's `Cow` it implements `From` 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` 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](https://docs.rs/maybe-owned).
## Example
Take a look at the [examples dir](./examples) 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`:
```rust
//... in a trait implementation
fn register(&mut self, key: SomeId, data: D)
where D: Into>
{
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
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
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](./CONTRIBUTORS.md)
Change Log
-----------
See [./CHANGELOG.md](./CHANGELOG.md)