mayber

Crates.iomayber
lib.rsmayber
version0.1.1
created_at2025-12-13 10:17:48.174728+00
updated_at2026-01-24 15:49:22.283109+00
descriptionA zero-cost enum for handling either references or owned values with a unified interface
homepagehttps://github.com/al8n/mayber
repositoryhttps://github.com/al8n/mayber
max_upload_size
id1982793
size68,225
Al Liu (al8n)

documentation

https://docs.rs/mayber

README

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

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, 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.

Commit count: 4

cargo fmt