nostd_cow 0.1.0

A no_std implementation of std::borrow::Cow
Documentation
  • Coverage
  • 100%
    11 out of 11 items documented2 out of 8 items with examples
  • Size
  • Source code size: 12.05 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.71 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Eisverygoodletter/nostd_cow
    2 2 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Eisverygoodletter

Std-less Cow

This library provides NoStdCow, which is an implementation of a copy-on-write smarter pointer which doesn't rely on std or alloc.

Crates.io Version docs.rs Crates.io License

If you have std or alloc available, use alloc::borrow::Cow instead. NoStdCow is more targeted towards embedded systems and alloc::borrow::Cow provides more functionality. into_alloc_cow and from_alloc_cow can be used to convert between the two if needed.

Overview

NoStdCow is defined as

pub enum NoStdCow<'a, T: Borrow<B>, B> {
    Owned(T),
    Borrowed(&'a B),
}

where &B is the borrowed form of T. In most cases, T == B and you will want to use NoStdCow<'a, T, T>. It is highly recommended that T also has a Clone implementation.

Example

use nostd_cow::NoStdCow;

/// Convert a string to uppercase if it isn't already uppercase, otherwise
/// just return a reference to the original source.
fn to_uppercase<'a>(source: &'a str) -> NoStdCow<'a, String, str> {
    for c in source.chars() {
        if !c.is_uppercase() {
            return NoStdCow::Owned(source.to_uppercase());
        }
    }
    NoStdCow::Borrowed(source)
}
// This string is already uppercase, so the function will not allocate a new [`String`].
let already_uppercase = "HELLOWORLD";
assert_eq!(to_uppercase(already_uppercase), NoStdCow::Borrowed(already_uppercase));
// This string needs to be converted to uppercase, so a new owned value is constructed
// and returned.
let not_uppercase = "helloworld";
assert_eq!(to_uppercase(not_uppercase), NoStdCow::Owned(String::from("HELLOWORLD")));