Crate derive_into_owned[][src]

Expand description


This crate supports deriving two different methods (not traits):

  • IntoOwned
  • Borrowed

These were first created to help out with types generated by quick_protobuf which generates structs with Cow fields. It is entirely possible that this crate is not needed at all and that there exists better alternatives to what this crate aims to support.

Definitions, naming

“Cow-alike” is used to mean a type that:

  • has a lifetime specifier, like Foo<'a>
  • has an implementation for fn into_owned(self) -> Foo<'static>

This is a bit of a bad name as Cow itself has a different kind of into_owned which returns the owned version of the generic type parameter. I am open to suggestions for both “Cow-alike” and into_owned.


#[derive(IntoOwned)] implements a method fn into_owned(self) -> Foo<'static> for type Foo<'a> which contains Cow or “Cow-alike” fields. The method returns a version with 'static lifetime which means the value owns all of it’s data. This is useful if you are for example, working with tokio-rs which currently requires types to be 'static.


#[derive(Borrowed)] implements a method fn borrowed<'b>(&'b self) -> Foo<'b> for type Foo<'a>. This is useful in case you need to transform the value into another type using std conversions like From, but you don’t want to clone the data in the process. Note that the all the fields that are not Cow or “Cow-alike” are just cloned, and new vectors are collected, so this yields savings only when you manage to save big chunks of memory.


Currently only the types I needed are supported and this might be a rather limited set of functionality. If you find that this does not work in your case please file an issue at project repository.

Derive Macros