pub struct Reference<T: ?Sized>(/* private fields */);Expand description
Implementations§
Source§impl<T: ?Sized> Reference<T>
impl<T: ?Sized> Reference<T>
Sourcepub const unsafe fn from_ptr(ptr: *mut T) -> Self
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.
Sourcepub const fn from_rc_ref_cell(rc_ref_cell: Rc<RefCell<T>>) -> Self
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.
Sourcepub const unsafe fn from_ptr_rw_lock(ptr_rw_lock: *const RwLock<T>) -> Self
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.
Sourcepub const unsafe fn from_ptr_mutex(ptr_mutex: *const Mutex<T>) -> Self
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.
Sourcepub const fn from_arc_rw_lock(arc_rw_lock: Arc<RwLock<T>>) -> Self
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.
Sourcepub const fn from_arc_mutex(arc_mutex: Arc<Mutex<T>>) -> Self
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.
Sourcepub fn into_inner(self) -> ReferenceUnsafe<T>
pub fn into_inner(self) -> ReferenceUnsafe<T>
Get the inner ReferenceUnsafe.
Sourcepub fn borrow(&self) -> Borrow<'_, T>
pub fn borrow(&self) -> Borrow<'_, T>
Examples found in repository?
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}Sourcepub fn borrow_mut(&self) -> BorrowMut<'_, T>
pub fn borrow_mut(&self) -> BorrowMut<'_, T>
Examples found in repository?
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}