pub trait ValueRef: Borrow<Self::Target> + Clone {
type Target: Eq + Clone;
}Expand description
A trait for references to Eq + Clone values, used by the SortedDisjointMap trait.
ValueRef enables SortedDisjointMap to map sorted, disjoint ranges of integers
to values of type V: Eq + Clone. It supports both references (&V) and shared ownership types
(Rc<V> and Arc<V>), avoiding unnecessary cloning of the underlying values while allowing ownership when needed.
References must also implement Clone. Standard reference types like &V, Rc<V>, and Arc<V>
implement Clone efficiently, as cloning typically involves copying a pointer.
§Motivation
Iterating over (range, value) pairs, such as with RangeMapBlaze::ranges, benefits from
references to values, which are cheap to clone. However, iterators like RangeMapBlaze::into_ranges
require ownership. By supporting Rc<Eq + Clone> and Arc<Eq + Clone>, ValueRef allows shared ownership,
freeing memory when the reference count drops to zero.
The following demonstrates the SortedDisjointMap::intersection operation working with
iterators of both (RangeInclusive<Integer>, &Eq + Clone) and (RangeInclusive<Integer>, Rc<Eq + Clone>).
(However, types cannot be mixed in the same operation due to Rust’s strong type system.)
use range_set_blaze::prelude::*;
use std::rc::Rc;
let a = RangeMapBlaze::from_iter([(2..=3, "a".to_string()), (5..=100, "a".to_string())]);
let b = RangeMapBlaze::from_iter([(3..=10, "b".to_string())]);
let mut c = a.range_values() & b.range_values();
assert_eq!(c.next(), Some((3..=3, &"a".to_string())));
assert_eq!(c.next(), Some((5..=10, &"a".to_string())));
assert_eq!(c.next(), None);
let mut c = a.into_range_values() & b.into_range_values();
assert_eq!(c.next(), Some((3..=3, Rc::new("a".to_string()))));
assert_eq!(c.next(), Some((5..=10, Rc::new("a".to_string()))));
assert_eq!(c.next(), None);Required Associated Types§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.