1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
use crate::free_lifetimes;
use core::any::{TypeId, Any, type_name};
/// A service provider pattern implementation = associated read-only container with type as a key.
///
/// Useful for building complex systems with callbacks without generic parameters.
///
/// # Examples
///
/// ```rust
/// mod call_back {
/// use dyn_context::state::State;
///
/// pub struct CallBack {
/// callback: Option<fn(state: &mut dyn State)>
/// }
///
/// impl CallBack {
/// pub fn new() -> Self { CallBack { callback: None } }
///
/// pub fn set_callback(&mut self, callback: fn(state: &mut dyn State)) {
/// self.callback.replace(callback);
/// }
///
/// pub fn call_back(&self, state: &mut dyn State) {
/// self.callback.map(|callback| callback(state));
/// }
/// }
/// }
///
/// use call_back::CallBack;
/// use dyn_context::state::{SelfState, StateExt};
/// use std::convert::Into;
///
/// struct PrintState {
/// value: String
/// }
///
/// impl SelfState for PrintState { }
///
/// # fn main() {
/// let mut call_back = CallBack::new();
/// call_back.set_callback(|state| {
/// let print: &PrintState = state.get();
/// println!("{}", &print.value);
/// });
/// call_back.call_back(&mut PrintState { value: "Hello, world!".into() });
/// # }
/// ```
///
/// For using `&str` instead of `String` the [`free_lifetimes!`](free_lifetimes!) macro can be used:
/// ```rust
/// # mod call_back {
/// # use dyn_context::state::State;
/// #
/// # pub struct CallBack {
/// # callback: Option<fn(state: &mut dyn State)>
/// # }
/// #
/// # impl CallBack {
/// # pub fn new() -> Self { CallBack { callback: None } }
/// #
/// # pub fn set_callback(&mut self, callback: fn(state: &mut dyn State)) {
/// # self.callback.replace(callback);
/// # }
/// #
/// # pub fn call_back(&self, state: &mut dyn State) {
/// # self.callback.map(|callback| callback(state));
/// # }
/// # }
/// # }
/// #
/// use call_back::CallBack;
/// use dyn_context::free_lifetimes;
/// use dyn_context::state::{SelfState, StateExt};
///
/// free_lifetimes! {
/// struct PrintState {
/// value: 'value ref str
/// }
/// }
///
/// impl SelfState for PrintState { }
///
/// # fn main() {
/// let mut call_back = CallBack::new();
/// call_back.set_callback(|state| {
/// let print: &PrintState = state.get();
/// println!("{}", print.value());
/// });
/// PrintStateBuilder {
/// value: "Hello, world!"
/// }.build_and_then(|state| call_back.call_back(state));
/// # }
/// ```
pub trait State: 'static {
/// Borrows shareable data entry.
///
/// Prefer high-level [`get`](StateExt::get) wrap.
fn get_raw(&self, ty: TypeId) -> Option<&dyn Any>;
/// Borrows mutable data entry.
///
/// Prefer high-level [`get_mut`](StateExt::get_mut) wrap.
fn get_mut_raw(&mut self, ty: TypeId) -> Option<&mut dyn Any>;
}
#[cfg(feature="nightly")]
impl State for ! {
fn get_raw(&self, _ty: TypeId) -> Option<&dyn Any> { Some(self) }
fn get_mut_raw(&mut self, _ty: TypeId) -> Option<&mut dyn Any> { Some(self) }
}
impl State for () {
fn get_raw(&self, _ty: TypeId) -> Option<&dyn Any> { None }
fn get_mut_raw(&mut self, _ty: TypeId) -> Option<&mut dyn Any> { None }
}
/// Marks implementor as a trivial [`State`](trait@State).
/// A trivial-implemented state is a state containing itself only.
///
/// # Examples
///
/// ```rust
/// # use dyn_context::state::{SelfState, State, StateExt};
/// #
/// struct SomeData {
/// data: u16,
/// }
///
/// impl SelfState for SomeData { }
///
/// fn get_data_from_state(state: &dyn State) -> u16 {
/// let some_data: &SomeData = state.get();
/// some_data.data
/// }
///
/// # fn main() {
/// let some_data = SomeData { data: 7 };
/// let data_from_state = get_data_from_state(&some_data);
/// assert_eq!(data_from_state, 7);
/// # }
pub trait SelfState: 'static { }
impl<T: SelfState> State for T {
fn get_raw(&self, ty: TypeId) -> Option<&dyn Any> {
if ty == TypeId::of::<T>() {
Some(self)
} else {
None
}
}
fn get_mut_raw(&mut self, ty: TypeId) -> Option<&mut dyn Any> {
if ty == TypeId::of::<T>() {
Some(self)
} else {
None
}
}
}
/// Extends [`State`](trait@State) with methods that make it easier to access the content of the state.
pub trait StateExt: State {
/// Borrows shareable data reference.
///
/// Panics if the state does not provide requested type.
fn get<T: 'static>(&self) -> &T {
self.get_raw(TypeId::of::<T>())
.unwrap_or_else(|| panic!("{} required", type_name::<T>()))
.downcast_ref::<T>().expect("invalid cast")
}
/// Borrows mutable data reference.
///
/// Panics if the state does not provide requested type.
fn get_mut<T: 'static>(&mut self) -> &mut T {
self.get_mut_raw(TypeId::of::<T>())
.unwrap_or_else(|| panic!("{} required", type_name::<T>()))
.downcast_mut::<T>().expect("invalid cast")
}
}
impl<T: State + ?Sized> StateExt for T { }
free_lifetimes! {
struct StateSum {
a: 'a ref dyn State,
b: 'b ref dyn State,
}
}
impl State for StateSum {
fn get_raw(&self, ty: TypeId) -> Option<&dyn Any> {
if let Some(r) = self.a().get_raw(ty) {
Some(r)
} else if let Some(r) = self.b().get_raw(ty) {
Some(r)
} else {
None
}
}
fn get_mut_raw(&mut self, _ty: TypeId) -> Option<&mut dyn Any> {
unreachable!()
}
}
free_lifetimes! {
struct StateSumMut {
a: 'a mut dyn State,
b: 'b mut dyn State,
}
}
impl State for StateSumMut {
fn get_raw(&self, ty: TypeId) -> Option<&dyn Any> {
if let Some(r) = self.a().get_raw(ty) {
Some(r)
} else if let Some(r) = self.b().get_raw(ty) {
Some(r)
} else {
None
}
}
fn get_mut_raw(&mut self, ty: TypeId) -> Option<&mut dyn Any> {
let r = if let Some(r) = self.a_mut().get_mut_raw(ty) {
Some(r as *mut _)
} else if let Some(r) = self.b_mut().get_mut_raw(ty) {
Some(r as *mut _)
} else {
None
};
r.map(|x| unsafe { &mut *x })
}
}
/// Provides method allowing combine two read-only [`State`](trait@State)s into one.
pub trait StateRef {
/// Merges two states into one and calls provided function with the combined state.
fn merge_and_then<T>(self, f: impl FnOnce(&dyn State) -> T, other: &dyn State) -> T;
}
impl<C: State> StateRef for &C {
fn merge_and_then<T>(self, f: impl FnOnce(&dyn State) -> T, other: &dyn State) -> T {
StateSumBuilder {
a: self,
b: other,
}.build_and_then(|x| f(x))
}
}
impl StateRef for &dyn State {
fn merge_and_then<T>(self, f: impl FnOnce(&dyn State) -> T, other: &dyn State) -> T {
StateSumBuilder {
a: self,
b: other,
}.build_and_then(|x| f(x))
}
}
/// Provides method allowing combine two [`State`](trait@State)s into one.
pub trait StateRefMut {
/// Merges two states into one and calls provided function with the combined state.
fn merge_mut_and_then<T>(self, f: impl FnOnce(&mut dyn State) -> T, other: &mut dyn State) -> T;
}
impl<C: State> StateRefMut for &mut C {
fn merge_mut_and_then<T>(self, f: impl FnOnce(&mut dyn State) -> T, other: &mut dyn State) -> T {
StateSumMutBuilder {
a: self,
b: other,
}.build_and_then(|x| f(x))
}
}
impl StateRefMut for &mut dyn State {
fn merge_mut_and_then<T>(self, f: impl FnOnce(&mut dyn State) -> T, other: &mut dyn State) -> T {
StateSumMutBuilder {
a: self,
b: other,
}.build_and_then(|x| f(x))
}
}