| Crates.io | mayber |
| lib.rs | mayber |
| version | 0.1.1 |
| created_at | 2025-12-13 10:17:48.174728+00 |
| updated_at | 2026-01-24 15:49:22.283109+00 |
| description | A zero-cost enum for handling either references or owned values with a unified interface |
| homepage | https://github.com/al8n/mayber |
| repository | https://github.com/al8n/mayber |
| max_upload_size | |
| id | 1982793 |
| size | 68,225 |
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.
Add this to your Cargo.toml:
[dependencies]
mayber = "0.1"
std (default): Standard library support with I/O traits (Read, Write, Seek, BufRead)alloc: Allocation support for no_std environmentsserde: 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"] }
MaybeMut)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 |
MaybeRef<T>: Deref, AsRef, Borrow, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug, DisplayMaybeMut<T>: Deref, DerefMut, AsRef, AsMut, Borrow, BorrowMut, I/O traits (std feature)Maybe<R, T>: Default, From<T>, From<&T>, Deserialize (serde feature)The original idea for this type comes from chumsky's util.rs.
This crate requires Rust 1.56 or later.
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.