pub trait Mapping {
type Dom: Eq + Clone;
type Cod: Eq + Clone;
// Required method
fn apply(&self, x: Self::Dom) -> Option<Self::Cod>;
// Provided methods
fn apply_to_ref(&self, x: &Self::Dom) -> Option<Self::Cod> { ... }
fn is_set(&self, x: &Self::Dom) -> bool { ... }
}
Expand description
A functional mapping.
A mapping sends values of type Dom
to values of type
Cod
. Unlike a function, a mapping need not be defined on its
whole domain. A mapping is thus more like a partial function, but it does not
even know its intended domain of definition, nor the codomain to which its image
should restrict. If needed, that information should be provided separately as
sets. Neither domain nor codomain are assumed to be finite.
This trait encompasses mappings that compute their values on the fly and
mappings that own their data, say in the form of a vector or hash map. Achieving
this flexiblity in Rust is delicate due to the sharp distinction between values
and references, but as a user, deciding which method to call is simple enough.
To evaluate at a point that you own and can consume, call
apply
. To evaluate at a point that you have only by reference
or can’t consume, call apply_to_ref
.
Required Associated Types§
Required Methods§
Provided Methods§
Sourcefn apply_to_ref(&self, x: &Self::Dom) -> Option<Self::Cod>
fn apply_to_ref(&self, x: &Self::Dom) -> Option<Self::Cod>
Applies the mapping at a reference to a point possibly in the domain.
The default implementation just calls apply
after cloning.
Mappings that own their data should give a more efficient implementation.
Sourcefn is_set(&self, x: &Self::Dom) -> bool
fn is_set(&self, x: &Self::Dom) -> bool
Is the mapping defined at a point?
The default implementation just checks whether
apply_to_ref
returns something, but a more efficient
implementation that avoids allocating should usually be given.