pub struct State<T>(pub T);Expand description
The app state passed to Router::with_state, or a part of it
that implements FromRef.
- Views borrow it:
State<&T>. They run synchronously within the frame, so they never pay for a clone, however big the state is. - Hooks and
loads own it:State<T>, a clone (henceT: Clone). Their futures can outlive the frame, but they only run once per navigation. Keep shared data behindRcanyway, so the clone is cheap and changes are visible everywhere.
A view asking for an owned State<T> doesn’t compile, so it can’t clone by accident:
ⓘ
fn view(ui: &mut egui::Ui, State(state): State<String>) {}
let page = Page::<String>::new(view); // `State<&String>` would workNeither can get &mut access: every view of the page (and every load) shares the state, and
a layout’s view still holds it while its page draws. Mutate it with Cell and RefCell.
use std::cell::Cell;
use std::rc::Rc;
use egui::Ui;
use egui_kit::Rejection;
use egui_kit::extract::State;
#[derive(Clone, Default)]
struct AppState {
clicks: Rc<Cell<u32>>,
}
async fn load(State(state): State<AppState>) -> Result<u32, Rejection> {
Ok(state.clicks.get())
}
fn view(ui: &mut Ui, State(state): State<&AppState>) {
if ui.button("Click").clicked() {
state.clicks.set(state.clicks.get() + 1);
}
}Tuple Fields§
§0: TTrait Implementations§
impl<T: Copy> Copy for State<T>
Auto Trait Implementations§
impl<T> Freeze for State<T>where
T: Freeze,
impl<T> RefUnwindSafe for State<T>where
T: RefUnwindSafe,
impl<T> Send for State<T>where
T: Send,
impl<T> Sync for State<T>where
T: Sync,
impl<T> Unpin for State<T>where
T: Unpin,
impl<T> UnsafeUnpin for State<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for State<T>where
T: UnwindSafe,
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> ⓘ
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 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> ⓘ
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