Struct rerun::MainThreadToken
source · pub struct MainThreadToken {
_dont_send_me: PhantomData<*const ()>,
}
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.
Fields§
§_dont_send_me: PhantomData<*const ()>
Implementations§
source§impl MainThreadToken
impl MainThreadToken
sourcepub fn i_promise_i_am_on_the_main_thread() -> MainThreadToken
pub fn i_promise_i_am_on_the_main_thread() -> MainThreadToken
Only call this from fn main
, or you may get weird runtime errors!
sourcepub fn from_egui_ui(_ui: &Ui) -> MainThreadToken
pub fn from_egui_ui(_ui: &Ui) -> MainThreadToken
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> CheckedAs for T
impl<T> CheckedAs for T
source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moresource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request