Skip to main content

SharedValue

Struct SharedValue 

Source
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§

Source§

impl SharedValue

Source

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.

Source

pub fn get_value(&self) -> f64

Get the value of the variable being shared

Source

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§

impl Debug for SharedValue

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for SharedValue

Source§

fn deserialize<D>(deserializer: D) -> Result<SharedValue, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for SharedValue

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Send for SharedValue

Source§

impl Sync for SharedValue

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,