Crate generic_mutability

Source
Expand description

This crate enables the creation of functions, methods, traits or even structs generic over mutability.

The main use case is allowing crates to write pairs of getter functions of the form

fn get<'a>(&'a T, ...) -> &'a U
fn get_mut<'a>(&'a mut T, ...) -> &'a mut U

as a single function

fn get_gen<'a, M: Mutability>(GenRef<'a, M, T>, ...) -> GenRef<'a, M, U>

The main items of this crate are the GenRef struct, which represents a safe reference (like & and &mut) that is generic over mutability; and the Mutability trait, which is used as a bound on generic mutability parameters.

Macros§

field
Maps a GenRef over field access and indexing.
gen_mut
This macro simplifies the implementation of generically mutable APIs, where the mutable and the shared code paths are (mostly) identical. It has the following syntax:

Structs§

GenRef
This is the main type of this crate. It is the generic-mutability equivalent of safe reference types (& and &mut).
IsMutable
The existence of a value of this type guarantees that a specific mutability parameter M is Mutable. Unsafe code may rely on this guarantee. You can obtain this value by matching over M::mutability().
IsShared
The existence of a value of this type guarantees that a specific mutability parameter M is Shared. Unsafe code may rely on this guarantee. You can obtain this value by matching over M::mutability().

Enums§

MutabilityEnum
This enum makes it possible to match over a mutability parameter. Not to be confused with the Mutability trait, which is used as a bound for mutability parameters; and Shared and Mutable, which are values of the mutability parameters.
Mutable
Represents the mutability of a mutable (unique) reference, &mut T.
Shared
Represents the mutability of a shared reference, &T.

Traits§

GenRefMethods
This trait allows you to call associated functions of GenRef with method syntax.
Mutability
This trait is used as a bound on generic mutability parameters.