Struct MainLoopScheduler

pub struct MainLoopScheduler(/* private fields */);
Expand description

MainLoopScheduler implements the Kanzi application main loop in form of a customizable sequence of stages, each consisting of a sequence of tasks, where a task is any callable, including functions, function objects, and lambdas.

It also provides the default Kanzi stages, along with platform specific mechanisms for automatic suspension, and frame rate control.

You can customize the application main loop by extending its stages with additional tasks, replacing existing tasks with your own implementations, or adding timer tasks to execute some functionality at regular intervals.

Implementations§

§

impl MainLoopScheduler

pub fn prepend_task( &self, stage: MainLoopStage, name: impl AsRef<KanziStr>, recurrence: TaskRecurrence, closure: impl Task, ) -> Result<MainLoopTaskToken, Error>

Adds a task to the front of a stage.

§Arguments
  • stage - Reference to the stage.
  • name - Name for the task.
  • recurrence - Recurrence choice for the task that you want to insert.
  • task - The task that you want to insert.
§Returns

Token of the added task. You can use that token to remove or replace the task.

pub fn append_task( &self, stage: MainLoopStage, name: impl AsRef<KanziStr>, recurrence: TaskRecurrence, closure: impl Task, ) -> Result<MainLoopTaskToken, Error>

Adds a task to the back of a stage.

§Arguments
  • stage - Reference to the stage.
  • name - Name for the task.
  • recurrence - Recurrence choice for the task that you want to insert.
  • task - The task that you want to insert.
§Returns

Token of the added task. You can use that token to remove or replace the task.

pub fn replace_task( &self, stage: MainLoopStage, token: &MainLoopTaskToken, closure: impl Task, ) -> Result<bool, Error>

Replaces a task in a stage with another one.

§Arguments
  • stage - Reference to the stage.
  • token - Token of the task.
  • task - The task with which to replace the current task.
§Returns

If replacement is successful, returns true. If the function cannot find the matching token, returns false.

pub fn remove_task( &self, stage: MainLoopStage, token: &MainLoopTaskToken, ) -> Result<bool, Error>

Removes a task from a stage.

§Arguments
  • stage - Reference to the stage.
  • token - Token of the task.
§Returns

If removal is successful, returns true. If the function cannot find the matching token, returns false.

pub fn clear_tasks(&self, stage: MainLoopStage) -> Result<(), Error>

Removes all tasks in a stage.

§Arguments
  • stage - Reference to the stage.
§

impl MainLoopScheduler

pub fn prepend_timer( &self, stage: MainLoopStage, name: impl AsRef<KanziStr>, recurrence: TimerRecurrence, interval: Duration, closure: impl TimerTask, ) -> Result<MainLoopTimerToken, Error>

Adds a timer task to be executed before a stage in the main loop. Use this overload when you do not have the task name available at compile time.

§Arguments
  • stage - Reference to the stage.
  • name - Name for the task.
  • recurrence - Recurrence type of the task.
  • interval - The amount of time after which to execute the task.
  • task - The task.
§Returns

Token of the added task. You can use this token to remove the task.

§Errors

Returns an INVALID_ARGUMENT error if the provided Duration doesn’t fit into i64.

pub fn append_timer( &self, stage: MainLoopStage, name: impl AsRef<KanziStr>, recurrence: TimerRecurrence, interval: Duration, closure: impl TimerTask, ) -> Result<MainLoopTimerToken, Error>

Adds a timer task to be executed after a stage in the main loop. Use this overload when you do not have the task name available at compile time.

§Arguments
  • stage - Reference to the stage.
  • name - Name for the task.
  • recurrence - Recurrence type of the task.
  • interval - The amount of time after which to execute the task.
  • task - The task.
§Returns

Token of the added task. You can use this token to remove the task.

§Errors

Returns an INVALID_ARGUMENT error if the provided Duration doesn’t fit into i64.

pub fn remove_timer(&self, token: &MainLoopTimerToken) -> Result<bool, Error>

Removes a timer task from the main loop.

§Arguments
  • token - Token of the task.
§Returns

If removal is successful, returns true. If the function cannot find the matching token, returns false.

§

impl MainLoopScheduler

pub fn set_current_frame_rendered(&self) -> Result<(), Error>

Sets whether rendering was performed in the current frame. Call this function from a task in the Render stage to inform the main loop that the task performed rendering. The main loop uses this as a precondition for executing the Present stage for a frame.

pub fn is_current_frame_rendered(&self) -> Result<bool, Error>

Returns whether rendering was performed in the current frame. The main loop uses this as a precondition for executing the Present stage for a frame.

§Returns

If rendering was performed in the current frame, true, otherwise false.

pub fn set_frame_rate_limit(&self, frame_rate: u32) -> Result<(), Error>

Sets the frame rate limit of the application. This sets an upper limit on the number of frames rendered by Kanzi every second.

§Arguments
  • frame_rate - The number of frames to render every second. To disable frame rate limit, use 0.

pub fn enable_suspend_when_idle( &self, suspend_enable: bool, ) -> Result<(), Error>

Sets whether an application allows suspension.

§Arguments
  • suspend_enable - If the application allows suspension, use true, otherwise false.

pub fn is_suspend_when_idle_enabled(&self) -> Result<bool, Error>

Returns whether an application allows suspension.

§Returns

If the application allows suspension, true, otherwise false.

pub fn pause(&self) -> Result<(), Error>

Pauses the main loop and changes its state to Paused.

let main_loop_scheduler = domain.main_loop_scheduler();
main_loop_scheduler.pause()?;
assert_eq!(main_loop_scheduler.get_state()?, kanzi::MainLoopState::Paused);

pub fn resume(&self) -> Result<(), Error>

Resumes the main loop and changes its state back to Running.

let main_loop_scheduler = domain.main_loop_scheduler();
main_loop_scheduler.resume()?;
assert_eq!(main_loop_scheduler.get_state()?, kanzi::MainLoopState::Running);

pub fn quit(&self) -> Result<(), Error>

Quits the main loop and changes its state to Quitting.

let main_loop_scheduler = domain.main_loop_scheduler();
main_loop_scheduler.quit()?;
assert_eq!(main_loop_scheduler.get_state()?, kanzi::MainLoopState::Quitting);

pub fn get_state(&self) -> Result<MainLoopState, Error>

Gets the main loop scheduler state.

Trait Implementations§

§

impl Clone for MainLoopScheduler

§

fn clone(&self) -> MainLoopScheduler

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AsAny for T
where T: 'static,

§

fn as_any(&self) -> &(dyn Any + 'static)

§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Inherits<T> for T

§

fn upcast(self) -> Base

§

fn upcast_ref(&self) -> &Base

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.