flex 0.1.0

Flexible borrowing and ownership for Rust
Documentation
  • Coverage
  • 100%
    6 out of 6 items documented3 out of 4 items with examples
  • Size
  • Source code size: 36.08 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.06 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • Homepage
  • npmccallum/flex
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • npmccallum

flex

CI Crates.io Documentation License: MIT

Flexible borrowing and ownership for Rust.

Flex is an enum that holds either a borrowed reference or an owned boxed value of the same type. It is similar in concept to Cow and works seamlessly with unsized types like dyn Trait, [T], and str.

Installation

[dependencies]
flex = "0.1"

If your crate has an alloc feature, pass it through to flex:

[features]
alloc = ["flex/alloc"]

[dependencies]
flex = "0.1"

Quick Start

use flex::Flex;

// Start with a borrowed slice
let borrowed = Flex::Lend(&[1, 2, 3][..]);
assert_eq!(&*borrowed, &[1, 2, 3]);

# #[cfg(feature = "alloc")] {
// Or own a slice
let owned = Flex::Give(vec![4, 5, 6].into_boxed_slice());
assert_eq!(&*owned, &[4, 5, 6]);

// Convert borrowed to owned
let claimed: Flex<'static, [i32]> = borrowed.claim();
# }

Flex vs Cow

While both Flex and Cow deal with borrowed vs owned data, they serve different purposes:

Cow (Clone-on-Write)

  • Works with type pairs (e.g., &str/String, &[T]/Vec<T>)
  • Uses ToOwned trait for conversion
  • Requires alloc - not available in no_std

Flex (Flexible Ownership)

  • Works with ownership models of the same type (&T vs Box<T>)
  • Works seamlessly with like unsized types dyn Trait, [u8] and str
  • No ToOwned requirement
  • Works in no_std without alloc - produces consistent APIs

Example: Flex<'a, str> holds either &'a str or Box<str>, while Cow<'a, str> holds either &'a str or String.

Use Cases

Flex is particularly useful when:

  • Working with trait objects: Flex<'a, dyn Debug> holds &dyn Debug or Box<dyn Debug>
  • Building APIs that accept both borrowed and owned unsized types
  • Flexible ownership without ToOwned constraints
  • Deferring allocation decisions until runtime

Features

  • alloc: Enables the Give variant with Box<T>

Without alloc, Flex only supports the Lend variant, but APIs remain compatible.

License

Licensed under the MIT License.