Skip to main content

Crate nostd_cow

Crate nostd_cow 

Source
Expand description

A no_std and no alloc Cow (copy-on-write) implementation.

This crate provides NoStdCow which is extremely similar to Cow except that it doesn’t rely on the ToOwned trait from alloc and uses Clone instead.

Use Cow from std/alloc instead if you have access to them. This library is useful for when you want Cow but don’t have alloc or std.

§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")));

Enums§

NoStdCow
A no_std clone-on-write smart pointer.

Type Aliases§

RefCow
A type alias of NoStdCow that can either store T or &T. If T is Clone, to_mut and into_owned will be available.