pub struct MainThreadData<T>(/* private fields */);Expand description
Data that can only be accessed from the main thread. Accessors take a MainThreadToken.
This can be moved freely between threads, but the Drop implementation will panic if it’s dropped from a thread other than the main thread.
Implementations§
Source§impl<T> MainThreadData<T>
impl<T> MainThreadData<T>
Sourcepub fn new(_: MainThreadToken, data: T) -> Self
pub fn new(_: MainThreadToken, data: T) -> Self
Create a new MainThreadData.
See also Self::assert_new()
Sourcepub fn get(&self, _: MainThreadToken) -> &T
pub fn get(&self, _: MainThreadToken) -> &T
Get shared access to this data.
See also Self::assert_get(), Self::get_on_main_thread()
Sourcepub fn get_mut(&mut self, _: MainThreadToken) -> &mut T
pub fn get_mut(&mut self, _: MainThreadToken) -> &mut T
Get exclusive access to this data.
See also Self::assert_get_mut(), Self::get_mut_on_main_thread()
Sourcepub fn assert_new(data: T) -> Self
pub fn assert_new(data: T) -> Self
Create a new MainThreadData. Panic if not called on the main thread.
See also Self::new()
Sourcepub fn assert_get(&self) -> &T
pub fn assert_get(&self) -> &T
Get shared access to this data. Panic if not called on the main thread.
See also Self::get(), Self::get_on_main_thread()
Sourcepub fn assert_get_mut(&mut self) -> &mut T
pub fn assert_get_mut(&mut self) -> &mut T
Get shared access to this data. Panic if not called on the main thread.
Sourcepub fn get_on_main_thread(&self, callback: impl FnOnce(&T) + Send) -> bool
pub fn get_on_main_thread(&self, callback: impl FnOnce(&T) + Send) -> bool
Get shared access to this data in a callback that’s run on the main thread. This method waits for the callback to complete before returning.
If this is called on a thread other than the main thread, it requires the SDL
event loop to run. See SDL_RunOnMainThread for details.
Returns false if the callback failed to run.
See also run_sync_on_main_thread(), Self::get(), Self::assert_get()
Sourcepub fn get_mut_on_main_thread(
&mut self,
callback: impl FnOnce(&mut T) + Send,
) -> bool
pub fn get_mut_on_main_thread( &mut self, callback: impl FnOnce(&mut T) + Send, ) -> bool
Get exclusive access to this data in a callback that’s run on the main thread. This method waits for the callback to complete before returning.
If this is called on a thread other than the main thread, it requires the SDL
event loop to run. See SDL_RunOnMainThread for details.
Returns false if the callback failed to run.
See also run_sync_on_main_thread(), Self::get_mut(), Self::assert_get_mut()