#cow #owned #maybe #version-string #borrowing

maybe-owned

provides a MaybeOwned (and MaybeOwnedMut) type similar to std’s Cow but it implements From<T> and From<&'a T> and does not require ToOwned

5 releases

Uses old Rust 2015

0.3.4 May 23, 2020
0.3.3 May 22, 2020
0.3.2 Sep 8, 2018
0.3.0 Sep 6, 2017
0.2.0 May 17, 2017

#162 in Rust patterns

Download history 39983/week @ 2023-12-15 24299/week @ 2023-12-22 35288/week @ 2023-12-29 42141/week @ 2024-01-05 50655/week @ 2024-01-12 54044/week @ 2024-01-19 55252/week @ 2024-01-26 56283/week @ 2024-02-02 59136/week @ 2024-02-09 55903/week @ 2024-02-16 67082/week @ 2024-02-23 61026/week @ 2024-03-01 60190/week @ 2024-03-08 56317/week @ 2024-03-15 65009/week @ 2024-03-22 51692/week @ 2024-03-29

244,652 downloads per month
Used in 264 crates (16 directly)

MIT/Apache

36KB
712 lines

maybe-owned Build Status 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.

Change Log

  • v0.3.4:

    • Added make_owned() as a to_mut() replacement, but also available for MaybeOwnedMut and more clear in it's functionality.
    • Added a as_mut() method to MaybeOwned which return a Option<&mut T>
    • Added missing BorrowMut implementation for MaybeOwnedMut
  • v0.3.3:

    • added MaybeOwnedMut
  • v0.3.2:

    • added transitive std::ops implementations
  • v0.3.1:

    • added serde support

Dependencies

~175KB