Crate mayber

Crate mayber 

Source
Expand description

mayber

A Rust library for handling either references or owned values with a unified interface.

github LoC Build codecov

docs.rs crates.io crates.io license

§Overview

mayber provides a flexible Maybe<R, T> type that can hold either a reference (R) or an owned value (T). This is particularly useful when you want to avoid unnecessary allocations or clones while maintaining the flexibility to work with owned data when necessary.

Unlike Cow<T> which is specifically designed for borrowed vs owned variants of the same type, Maybe<R, T> is more generic and works with any reference type and owned type pair.

§Installation

Add this to your Cargo.toml:

[dependencies]
mayber = "0.1"

§Features

  • std (default): Standard library support with I/O traits (Read, Write, Seek, BufRead)
  • alloc: Allocation support for no_std environments
  • serde: Serialize/Deserialize implementations
# With serde
[dependencies]
mayber = { version = "0.1", features = ["serde"] }

# no_std with alloc
mayber = { version = "0.1", default-features = false, features = ["alloc"] }

§When to Use

  • Accept both references and owned values without cloning
  • Build flexible APIs that work with borrowed or owned data
  • Need mutable reference or owned value flexibility (MaybeMut)
  • Reduce allocations in performance-critical paths

§Comparison with Cow

FeatureCow<'a, T>Maybe<R, T>
PurposeClone-on-write for specific typesGeneric reference or owned value
Type flexibilityLimited to ToOwned typesWorks with any R and T pair
Common use caseCow<'a, str>, Cow<'a, [T]>MaybeRef<'a, T>, MaybeMut<'a, T>
Deref behaviorAlways derefs to borrowed formMaybeRef/MaybeMut implement Deref/DerefMut

§Trait Implementations

  • MaybeRef<T>: Deref, AsRef, Borrow, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug, Display
  • MaybeMut<T>: Deref, DerefMut, AsRef, AsMut, Borrow, BorrowMut, I/O traits (std feature)
  • Maybe<R, T>: Default, From<T>, From<&T>, Deserialize (serde feature)

§Credit

The original idea for this type comes from chumsky’s util.rs.

§Minimum Supported Rust Version (MSRV)

This crate requires Rust 1.56 or later.

§License

mayber is under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE, LICENSE-MIT for details.

Copyright (c) 2025 Al Liu.

Re-exports§

pub use crate::Maybe::Owned;
pub use crate::Maybe::Ref;

Enums§

Maybe
An enum that can hold either a reference or an owned value.

Type Aliases§

MaybeMut
A type alias for a Maybe enum that holds either a mutable reference or an owned value.
MaybeRef
A type alias for a Maybe enum that holds either a reference or an owned value.