pub trait TupleSet {
// Required methods
fn count<T: 'static>(&self) -> usize;
unsafe fn get_unchecked<T: 'static>(&self) -> &T;
unsafe fn map_unchecked<T: 'static, F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut T) -> R;
// Provided methods
fn contains_unique<T: 'static>(&self) -> bool { ... }
fn set<T: 'static>(&mut self, value: T) -> Option<T> { ... }
fn get<T: 'static>(&self) -> Option<&T> { ... }
unsafe fn set_unchecked<T: 'static>(&mut self, value: T) { ... }
fn map<T: 'static, F, R>(&mut self, f: F) -> Option<R>
where F: FnOnce(&mut T) -> R { ... }
fn take<T: 'static + Default>(&mut self) -> Option<T> { ... }
}Expand description
Trait for accessing and manipulating tuple elements by type.
Required Methods§
Sourceunsafe fn get_unchecked<T: 'static>(&self) -> &T
unsafe fn get_unchecked<T: 'static>(&self) -> &T
Get a reference to the value for type T in the tuple without checking.
§Safety
The caller must ensure that T appears exactly once in the tuple.
Calling this with a type that doesn’t exist will panic, while calling
this with a type that appears multiple times will return a reference to
the first occurrence.
Sourceunsafe fn map_unchecked<T: 'static, F, R>(&mut self, f: F) -> R
unsafe fn map_unchecked<T: 'static, F, R>(&mut self, f: F) -> R
Applies a mapping function to the value of type T in the tuple without
checking.
§Safety
The caller must ensure that T appears exactly once in the tuple.
Calling this with a type that doesn’t exist will panic, while calling
this with a type that appears multiple times may lead to solely changing
the first occurrence.
§Examples
use tuple_set::TupleSet;
let mut tuple = (42i32, "hello", 3.14f64);
let old = unsafe {
tuple.map_unchecked(|x: &mut i32| {
let old = *x;
*x *= 2;
old
})
};
assert_eq!(old, 42);
assert_eq!(tuple.0, 84);Provided Methods§
Sourcefn contains_unique<T: 'static>(&self) -> bool
fn contains_unique<T: 'static>(&self) -> bool
Returns true if the tuple contains exactly one instance of type T.
Sourcefn set<T: 'static>(&mut self, value: T) -> Option<T>
fn set<T: 'static>(&mut self, value: T) -> Option<T>
Sets the value for type T if it appears exactly once in the tuple.
Returns None on success, Some(value) if the type is not found or
appears multiple times.
§Examples
use tuple_set::TupleSet;
let mut tuple = (42i32, "hello", 3.14f64);
assert!(tuple.set(100i32).is_none());
assert_eq!(tuple.0, 100);
// Type not found
let result = tuple.set(true);
assert_eq!(result, Some(true));Sourcefn get<T: 'static>(&self) -> Option<&T>
fn get<T: 'static>(&self) -> Option<&T>
Get a reference to the value for type T in the tuple if it appears
exactly once.
Returns Some(&T) on success, or None if the type is not found or
appears multiple times.
§Examples
use tuple_set::TupleSet;
let tuple = (42i32, "hello", 3.14f64);
let value: Option<&i32> = tuple.get();
assert_eq!(value, Some(&42i32));Sourceunsafe fn set_unchecked<T: 'static>(&mut self, value: T)
unsafe fn set_unchecked<T: 'static>(&mut self, value: T)
Sets the value for type T in the tuple without checking.
§Safety
The caller must ensure that T appears exactly once in the tuple.
Calling this with a type that doesn’t exist or appears multiple times
may lead to respectively to a panic or changing solely the first
occurrence.
§Examples
use tuple_set::TupleSet;
let mut tuple = (42i32, "hello", 3.14f64);
unsafe {
tuple.set_unchecked(200i32);
}
assert_eq!(tuple.0, 200);Sourcefn map<T: 'static, F, R>(&mut self, f: F) -> Option<R>
fn map<T: 'static, F, R>(&mut self, f: F) -> Option<R>
Applies a mapping function to the value of type T in the tuple.
Returns Some(result) with the function’s return value on success,
or None if the type is not found or appears multiple times.
§Examples
use tuple_set::TupleSet;
let mut tuple = (42i32, "hello", 3.14f64);
let old = tuple
.map(|x: &mut i32| {
let old = *x;
*x *= 2;
old
})
.unwrap();
assert_eq!(old, 42);
assert_eq!(tuple.0, 84);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 TupleSet for ()
impl TupleSet for ()
Source§fn count<Target: 'static>(&self) -> usize
fn count<Target: 'static>(&self) -> usize
Always returns 0 since the tuple is empty.
§Examples
use tuple_set::TupleSet;
let tuple = ();
assert_eq!(tuple.count::<i32>(), 0);