IntoOwned trait for Rust
This crate provides the IntoOwned trait, which converts a
borrowed form of a type into its owned form.
Motivation
A common pattern in performance-sensitive code is to maintain two parallel representations of a type: a borrowed form that holds references into existing data (cheap to create and copy), and an owned form that carries its own heap-allocated data (can outlive the source).
// Cheap to push onto a stack during processing — no allocation.
// Stored in the final result — heap-allocated, lifetime-free.
let frame = Finished;
let owned = frame.into_owned;
IntoOwned gives generic code a single, uniform interface for this
conversion, regardless of how complex the borrowed type's internal
structure is.
Built-in implementations
Two implementations are provided out of the box as convenience building blocks when implementing the trait for your own types:
&TwhereT:ToOwned(delegates to [ToOwned::to_owned])- [
Cow<'a, T>][std::borrow::Cow] whereT:ToOwned(delegates to [Cow::into_owned])
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (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.