pub struct SharedValue { /* private fields */ }Expand description
A pointer to a value so it can be modified in many places
Rust’s ownership rules preclude multiple mutable borrows from taking place. The basis interface is designed around the assumption that this is possible. This provides an abstraction around handling a value that is accessible in multiple locations.
This abstracts away the implementation details, allowing for a range of different methods to be tested and implemented. The current implementation, based on using unsafe pointers, has the best performance by a significant factor.
use crystal_packing::SharedValue;
let x = SharedValue::new(1.);
let shared_x = &x;
assert!(shared_x.get_value() == 1.);
assert!(shared_x.get_value() == x.get_value());
// Updating the SharedValue will update the linked variable
shared_x.set_value(2.);
assert!(shared_x.get_value() == x.get_value());
assert!(shared_x.get_value() == 2.);
assert!(x.get_value() == 2.);
Implementations§
Sourcepub fn new(val: f64) -> SharedValue
pub fn new(val: f64) -> SharedValue
Create a SharedValue allowing modification of the given value
§Arguments
val- A reference to a f64 value which can be updated in multiple locations
§Remarks
This provides a highly performant access to modifying the value of a variable in multiple locations.
modifying the value will result in a runtime memory fault. An alternative implementation
which takes &mut f64 would not suffer from the same issues, however this then has issues
with mutability of lifetimes.
Sourcepub fn set_value(&self, value: f64)
pub fn set_value(&self, value: f64)
This updates the value which is being shared
§Arguments
value- The new value to assign to the shared value
§Remarks
This breaks the single mutability rules of rust, and is consequently unsafe to use in threaded code.
§Example
use crystal_packing::SharedValue;
let x = SharedValue::new(1.);
let shared_x = &x;
// Update the value of x through shared_x
shared_x.set_value(2.);
// The values of both x and shared_x will be updated
assert_eq!(x.get_value(), 2.);
assert_eq!(shared_x.get_value(), 2.);Trait Implementations§
Source§fn deserialize<D>(deserializer: D) -> Result<SharedValue, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<SharedValue, D::Error>where
D: Deserializer<'de>,
Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.