pub trait TupleSet<T> {
// Required methods
fn set(&mut self, value: T) -> Option<T>;
fn get(&self) -> Option<&T>;
unsafe fn set_unchecked(&mut self, value: T);
unsafe fn get_unchecked(&self) -> &T;
fn map<F, R>(&mut self, f: F) -> Option<R>
where F: FnOnce(&mut T) -> R;
unsafe fn map_unchecked<F, R>(&mut self, f: F) -> R
where F: FnOnce(&mut T) -> R;
// Provided method
fn take(&mut self) -> Option<T>
where T: Default { ... }
}Expand description
Trait for setting values in tuples.
Required Methods§
Sourcefn set(&mut self, value: T) -> Option<T>
fn set(&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(&self) -> Option<&T>
fn get(&self) -> Option<&T>
Get a reference to the value for type T in the tuple if it appears
exactly once.
Sourceunsafe fn set_unchecked(&mut self, value: T)
unsafe fn set_unchecked(&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
will panic.
§Examples
use tuple_set::TupleSet;
let mut tuple = (42i32, "hello", 3.14f64);
unsafe {
tuple.set_unchecked(200i32);
}
assert_eq!(tuple.0, 200);Sourceunsafe fn get_unchecked(&self) -> &T
unsafe fn get_unchecked(&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.
Sourcefn map<F, R>(&mut self, f: F) -> Option<R>
fn map<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);Sourceunsafe fn map_unchecked<F, R>(&mut self, f: F) -> R
unsafe fn map_unchecked<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 or appears multiple times
will panic.
§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§
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.