Reference

Struct Reference 

Source
pub struct Reference<T: ?Sized>(/* private fields */);
Expand description

A container privately holding an enum with variants containing different kinds of references, the availability of some of which depends on crate features. Reference is borrowed like a RefCell. It is also reexported at the crate level.

Implementations§

Source§

impl<T: ?Sized> Reference<T>

Source

pub const unsafe fn from_ptr(ptr: *mut T) -> Self

Create a Reference from a raw mutable pointer. This is useful if you are not multithreading and you want to avoid dynamic allocation. Making the target static is recommended. The static_reference! macro is a convenient way of making an object static and getting a Reference of the raw pointer variant to it. Because the object is guaranteed to be static, it can be called without an unsafe block.

Source

pub const fn from_rc_ref_cell(rc_ref_cell: Rc<RefCell<T>>) -> Self

Create a Reference from an Rc<RefCell<T>>. The rc_ref_cell_reference function is a convenient way of putting an object in an Rc<RefCell<T>> and getting a Reference of this variant to it.

Source

pub const unsafe fn from_ptr_rw_lock(ptr_rw_lock: *const RwLock<T>) -> Self

Create a Reference from a *const RwLock<T>. Making the RwLock itself static is recommended. The static_rw_lock_reference! macro is a convenient way of putting an object in a static RwLock and getting a Reference of this variant to it.

Source

pub const unsafe fn from_ptr_mutex(ptr_mutex: *const Mutex<T>) -> Self

Create a Reference from a *const Mutex<T>. Making the Mutex itself static is recommended. The static_mutex_reference! macro is a convenient way of putting an object in a static Mutex and getting a Reference of this variant to it.

Source

pub const fn from_arc_rw_lock(arc_rw_lock: Arc<RwLock<T>>) -> Self

Create a Reference from an Arc<RwLock<T>>. The arc_rw_lock_reference function is a convenient way of putting an object in an Arc<RwLock> and getting a Reference of this variant to it.

Source

pub const fn from_arc_mutex(arc_mutex: Arc<Mutex<T>>) -> Self

Create a Reference from an Arc<Mutex<T>>. The arc_mutex_reference function is a convenient way of putting an object in an Arc<Mutex> and getting a Reference of this variant to it.

Source

pub fn into_inner(self) -> ReferenceUnsafe<T>

Get the inner ReferenceUnsafe.

Source

pub fn borrow(&self) -> Borrow<'_, T>

Immutably borrow the Reference like a RefCell.

Examples found in repository?
examples/pid.rs (line 161)
144fn main() {
145    const SETPOINT: Quantity = Quantity::new(5.0, MILLIMETER);
146    const KP: Quantity = Quantity::dimensionless(1.0);
147    const KI: Quantity = Quantity::dimensionless(0.01);
148    const KD: Quantity = Quantity::dimensionless(0.1);
149    println!("PID Controller using RRTK Streams");
150    println!(
151        "kp = {:?}; ki = {:?}; kd = {:?}",
152        KP.value, KI.value, KD.value
153    );
154    let input = to_dyn!(Getter<Quantity, ()>, rc_ref_cell_reference(MyStream::new()));
155    let mut stream = StreamPID::new(input.clone(), SETPOINT, KP, KI, KD);
156    stream.update().unwrap();
157    println!(
158        "time: {:?}; setpoint: {:?}; process: {:?}; command: {:?}",
159        stream.get().unwrap().unwrap().time.0,
160        SETPOINT.value,
161        input.borrow().get().unwrap().unwrap().value.value,
162        stream.get().unwrap().unwrap().value
163    );
164    for _ in 0..6 {
165        input.borrow_mut().update().unwrap();
166        stream.update().unwrap();
167        println!(
168            "time: {:?}; setpoint: {:?}; process: {:?}; command: {:?}",
169            stream.get().unwrap().unwrap().time,
170            SETPOINT,
171            input.borrow().get().unwrap().unwrap().value,
172            stream.get().unwrap().unwrap().value
173        );
174    }
175}
Source

pub fn borrow_mut(&self) -> BorrowMut<'_, T>

Mutably borrow the Reference like a RefCell.

Examples found in repository?
examples/pid.rs (line 105)
100    fn update(&mut self) -> NothingOrError<()> {
101        //The other streams used that are not updated here do not need to be updated. Streams like
102        //SumStream just calculate their output in the get method since they do not need to store
103        //any data beyond the `Reference`s to their inputs. The non-math streams used here work in
104        //a similar way.
105        self.int.borrow_mut().update()?;
106        self.drv.borrow_mut().update()?;
107        self.pro_float_maker.borrow_mut().update()?;
108        self.int_float_maker.borrow_mut().update()?;
109        self.drv_float_maker.borrow_mut().update()?;
110        Ok(())
111    }
112}
113#[cfg(feature = "alloc")]
114struct MyStream {
115    time: Time,
116}
117#[cfg(feature = "alloc")]
118impl MyStream {
119    pub fn new() -> Self {
120        Self { time: Time(0) }
121    }
122}
123//In a real system, obviously, the process variable must be dependent on the command. This is a
124//very rudimentary placeholder and a poor model of an actual system. All this example is
125//intended to do is to show the PID controller's command values and not model a real system by
126//assuming a constant velocity.
127#[cfg(feature = "alloc")]
128impl Getter<Quantity, ()> for MyStream {
129    fn get(&self) -> Output<Quantity, ()> {
130        Ok(Some(Datum::new(
131            self.time,
132            Quantity::from(self.time) * Quantity::new(0.5, MILLIMETER_PER_SECOND),
133        )))
134    }
135}
136#[cfg(feature = "alloc")]
137impl Updatable<()> for MyStream {
138    fn update(&mut self) -> NothingOrError<()> {
139        self.time += Time(2_000_000_000);
140        Ok(())
141    }
142}
143#[cfg(feature = "alloc")]
144fn main() {
145    const SETPOINT: Quantity = Quantity::new(5.0, MILLIMETER);
146    const KP: Quantity = Quantity::dimensionless(1.0);
147    const KI: Quantity = Quantity::dimensionless(0.01);
148    const KD: Quantity = Quantity::dimensionless(0.1);
149    println!("PID Controller using RRTK Streams");
150    println!(
151        "kp = {:?}; ki = {:?}; kd = {:?}",
152        KP.value, KI.value, KD.value
153    );
154    let input = to_dyn!(Getter<Quantity, ()>, rc_ref_cell_reference(MyStream::new()));
155    let mut stream = StreamPID::new(input.clone(), SETPOINT, KP, KI, KD);
156    stream.update().unwrap();
157    println!(
158        "time: {:?}; setpoint: {:?}; process: {:?}; command: {:?}",
159        stream.get().unwrap().unwrap().time.0,
160        SETPOINT.value,
161        input.borrow().get().unwrap().unwrap().value.value,
162        stream.get().unwrap().unwrap().value
163    );
164    for _ in 0..6 {
165        input.borrow_mut().update().unwrap();
166        stream.update().unwrap();
167        println!(
168            "time: {:?}; setpoint: {:?}; process: {:?}; command: {:?}",
169            stream.get().unwrap().unwrap().time,
170            SETPOINT,
171            input.borrow().get().unwrap().unwrap().value,
172            stream.get().unwrap().unwrap().value
173        );
174    }
175}

Trait Implementations§

Source§

impl<T: ?Sized> Clone for Reference<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: ?Sized> From<Reference<T>> for ReferenceUnsafe<T>

Source§

fn from(was: Reference<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T> Freeze for Reference<T>
where T: ?Sized,

§

impl<T> !RefUnwindSafe for Reference<T>

§

impl<T> !Send for Reference<T>

§

impl<T> !Sync for Reference<T>

§

impl<T> Unpin for Reference<T>
where T: ?Sized,

§

impl<T> !UnwindSafe for Reference<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.