Skip to main content

Modify

Trait Modify 

Source
pub trait Modify<K, Q: ?Sized = K>: Container
where K: Borrow<Q>,
{ // Required method fn modify<F>(&mut self, key: &Q, f: F) where F: FnOnce(&mut Self::Value); }
Expand description

Modify the value under key with a closure.

This is useful if something always has to be done before or after the modification to maintain an invariant.

§Examples

use maplike::ops::{Get, Modify};
use std::collections::{BTreeMap, HashMap};

// Works for `Vec`.
let mut vec = vec![1.0, 2.0, 3.0];
vec.modify(&1, |value| *value *= 10.0);
assert_eq!(vec.get(&1), Some(&20.0));

// Works for `[T; N]`.
let mut arr = [1.0, 2.0, 3.0];
arr.modify(&1, |value| *value *= 10.0);
assert_eq!(arr.get(&1), Some(&20.0));

// Works for `HashMap`.
let mut hash_map = HashMap::from([(1, 2.0), (2, 3.0)]);
hash_map.modify(&1, |value| *value *= 10.0);
assert_eq!(hash_map.get(&1), Some(&20.0));

// Works for `BTreeMap`.
let mut btree_map = BTreeMap::from([(1, 2.0), (2, 3.0)]);
btree_map.modify(&1, |value| *value *= 10.0);
assert_eq!(btree_map.get(&1), Some(&20.0));

Required Methods§

Source

fn modify<F>(&mut self, key: &Q, f: F)
where F: FnOnce(&mut Self::Value),

Modify the value under key with a closure.

This is useful if something always has to be done before or after the modification to maintain an invariant.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Modify<usize> for ()

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for bool

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for char

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for f32

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for f64

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for i8

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for i16

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for i32

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for i64

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for i128

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for isize

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for u8

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for u16

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for u32

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for u64

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for u128

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl Modify<usize> for usize

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut Self),

Source§

impl<A: Array> Modify<usize> for ArrayVec<A>

Available on crate feature tinyvec only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut A::Item),

Source§

impl<A: Array> Modify<usize> for SmallVec<A>

Available on crate feature smallvec only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut A::Item),

Source§

impl<A: Array> Modify<usize> for TinyVec<A>

Available on crate feature tinyvec only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut A::Item),

Source§

impl<K, Q: Eq + Hash + ?Sized, V> Modify<K, Q> for HashMap<K, V>
where K: Borrow<Q> + Eq + Hash,

Available on crate feature std only.
Source§

fn modify<F>(&mut self, key: &Q, f: F)
where F: FnOnce(&mut V),

Source§

impl<K, Q: Eq + Hash + ?Sized, V> Modify<K, Q> for IndexMap<K, V>
where K: Borrow<Q> + Eq + Hash,

Available on crate feature indexmap only.
Source§

fn modify<F>(&mut self, key: &Q, f: F)
where F: FnOnce(&mut V),

Source§

impl<K, Q: Ord + ?Sized, V> Modify<K, Q> for BTreeMap<K, V>
where K: Borrow<Q> + Ord,

Available on crate feature alloc only.
Source§

fn modify<F>(&mut self, key: &Q, f: F)
where F: FnOnce(&mut V),

Source§

impl<T, const CAP: usize> Modify<usize> for ArrayVec<T, CAP>

Available on crate feature arrayvec only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut T),

Source§

impl<V, C: Core<V>> Modify<usize> for StableVecFacade<V, C>

Available on crate feature stable-vec only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Source§

impl<V, const N: usize> Modify<usize> for [V; N]

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Source§

impl<V> Modify<Index> for Arena<V>

Available on crate feature thunderdome only.
Source§

fn modify<F>(&mut self, key: &Index, f: F)
where F: FnOnce(&mut V),

Source§

impl<V> Modify<usize> for Arc<V>

Available on crate feature std only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Source§

impl<V> Modify<usize> for Box<V>

Available on crate feature alloc only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Source§

impl<V> Modify<usize> for Option<V>

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Source§

impl<V> Modify<usize> for Rc<V>

Available on crate feature alloc only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Source§

impl<V> Modify<usize> for Vec<V>

Available on crate feature alloc only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Source§

impl<V> Modify<usize> for VecDeque<V>

Available on crate feature alloc only.
Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Source§

impl<V> Modify<usize> for [V]

Source§

fn modify<F>(&mut self, index: &usize, f: F)
where F: FnOnce(&mut V),

Implementors§

Source§

impl<V> Modify<usize> for One<V>