1 unstable release
| 0.1.1 | Jan 24, 2026 |
|---|---|
| 0.1.0 |
|
#1283 in Rust patterns
969 downloads per month
Used in tokit
44KB
661 lines
mayber
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 forno_stdenvironmentsserde:Serialize/Deserializeimplementations
# 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
| Feature | Cow<'a, T> |
Maybe<R, T> |
|---|---|---|
| Purpose | Clone-on-write for specific types | Generic reference or owned value |
| Type flexibility | Limited to ToOwned types |
Works with any R and T pair |
| Common use case | Cow<'a, str>, Cow<'a, [T]> |
MaybeRef<'a, T>, MaybeMut<'a, T> |
| Deref behavior | Always derefs to borrowed form | MaybeRef/MaybeMut implement Deref/DerefMut |
Trait Implementations
MaybeRef<T>:Deref,AsRef,Borrow,PartialEq,Eq,PartialOrd,Ord,Clone,Copy,Hash,Debug,DisplayMaybeMut<T>:Deref,DerefMut,AsRef,AsMut,Borrow,BorrowMut, I/O traits (stdfeature)Maybe<R, T>:Default,From<T>,From<&T>,Deserialize(serdefeature)
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.
Dependencies
~115KB