Crate dairy

Source
Expand description

A more compact, user friendly clone-on-write smart pointer.

use dairy::Cow;
let borrowed: Cow<str> = Cow::borrowed("Hello World!");
let owned: Cow<str> = Cow::owned(String::from("Hello World!"));

dairy::Cow is an improved version of the standard library std::borrow::Cow. Depending on the platform and type this crate provides a better underlying implementation which will be more compact. This crate currently supports the following types: str, [T], CStr, OsStr, and Path.

dairy::Cow is also able to provide many more From implementations; some which are not possible for the standard library to provide due to the alloc, std split. For example Cow<Path> now has the useful From<&str> implementation.

§Underlying implementation

  • On 64-bit platforms the compact implementation of Cow is two words wide, storing the length, capacity, and the ownership tag in the same word.
  • On 32-bit platforms the compact implementation of Cow is three words wide, storing the capacity and the ownership tag in the same word.
  • The default implementation simply uses the the standard library implementation which is four words wide. This is typically required in cases where the standard library does not provide an .into_raw_parts() or equivalent method for the owned version of types.

The following table documents how Cow<T> is implemented for each type on depending on the platform.

Cow<T>Unix/WASIOther
Cow<str>compactcompact
Cow<[T]>compactcompact
Cow<CStr>compactcompact
Cow<OsStr>compactdefault
Cow<Path>compactdefault

Structs§

  • A clone-on-write smart pointer.

Traits§

  • Internal trait which allows us to have different Cow implementations for the same type across different platforms.
  • Converts the owned version of self into boxed data.

Type Aliases§

  • Convenient type alias for a clone-on-write CStr.
  • Convenient type alias for a clone-on-write OsStr.
  • Convenient type alias for a clone-on-write Path.
  • Convenient type alias for a clone-on-write str.
  • Convenient type alias for a clone-on-write [T].