Trait ValueRef

Source
pub trait ValueRef: Borrow<Self::Target> + Clone {
    type Target: Eq + Clone;

    // Required method
    fn to_owned(self) -> Self::Target;
}
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([(3..=10, "a".to_string())]);
let b = RangeMapBlaze::from_iter([(2..=3, "b".to_string()), (5..=100, "b".to_string())]);

let mut c = a.range_values() & b.range_values();
assert_eq!(c.next(), Some((3..=3, &"b".to_string())));
assert_eq!(c.next(), Some((5..=10, &"b".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("b".to_string()))));
assert_eq!(c.next(), Some((5..=10, Rc::new("b".to_string()))));
assert_eq!(c.next(), None);

Required Associated Types§

Source

type Target: Eq + Clone

The Eq + Clone value type to which the reference points.

Required Methods§

Source

fn to_owned(self) -> Self::Target

Converts a reference or shared pointer to an owned value of type Self::Target.

This method allows values of type Self (e.g., &V, Rc<V>, or Arc<V>) to be turned into a fully owned V, which is required when consuming or storing values independently.

  • For plain references (&V), this clones the referenced value.
  • For Rc<V> and Arc<V>, this attempts to unwrap the value if uniquely owned; otherwise, it clones the inner value.

This method is typically used when converting a stream of (range, value) pairs into owned data, such as when building a new RangeMapBlaze from an iterator.

§Example
use std::rc::Rc;
use range_set_blaze::ValueRef;

let rc = Rc::new("hello".to_string());
let owned: String = rc.to_owned(); // avoids cloning if ref count is 1

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.

Implementations on Foreign Types§

Source§

impl<V> ValueRef for &V
where V: Eq + Clone,

Source§

type Target = V

Source§

fn to_owned(self) -> Self::Target

Source§

impl<V> ValueRef for Rc<V>
where V: Eq + Clone,

Source§

type Target = V

Source§

fn to_owned(self) -> Self::Target

Source§

impl<V> ValueRef for Arc<V>
where V: Eq + Clone,

Source§

type Target = V

Source§

fn to_owned(self) -> Self::Target

Implementors§