pub struct MainThreadToken { /* private fields */ }Expand description
A token that (almost) proves we are on the main thread.
Certain operations are only allowed on the main thread.
These operations should require this token.
For instance, any function using file dialogs (e.g. using rfd) should require this token.
The token should only be constructed in fn main, using MainThreadToken::i_promise_i_am_on_the_main_thread,
and then be passed down the call tree to where it is needed.
MainThreadToken is neither Send nor Sync,
thus guaranteeing that it cannot be found in other threads.
Of course, there is nothing stopping you from calling MainThreadToken::i_promise_i_am_on_the_main_thread from a background thread,
but PLEASE DON’T DO THAT.
In other words, don’t use this as a guarantee for unsafe code.
There is also MainThreadToken::from_egui_ui which uses the implicit guarantee of egui
(which usually is run on the main thread) to construct a MainThreadToken.
Use this only in a code base where you are sure that egui is running only on the main thread.
Implementations§
Source§impl MainThreadToken
impl MainThreadToken
Sourcepub fn i_promise_i_am_on_the_main_thread() -> Self
pub fn i_promise_i_am_on_the_main_thread() -> Self
Only call this from fn main, or you may get weird runtime errors!
Sourcepub fn from_egui_ui(_ui: &Ui) -> Self
pub fn from_egui_ui(_ui: &Ui) -> Self
We should only create an egui::Ui on the main thread,
so having it is good enough to “prove” that we are on the main thread.
Use this only in a code base where you are sure that egui is running only on the main thread.
In theory there is nothing preventing anyone from creating a egui::Ui on another thread,
but practice that is unlikely (or intentionally malicious).
Trait Implementations§
Source§impl Clone for MainThreadToken
impl Clone for MainThreadToken
Source§fn clone(&self) -> MainThreadToken
fn clone(&self) -> MainThreadToken
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for MainThreadToken
Auto Trait Implementations§
impl Freeze for MainThreadToken
impl RefUnwindSafe for MainThreadToken
impl !Send for MainThreadToken
impl !Sync for MainThreadToken
impl Unpin for MainThreadToken
impl UnwindSafe for MainThreadToken
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
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>
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>
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