Struct Looper

Source
pub struct Looper<A>
where A: Send + 'static,
{ /* private fields */ }
Expand description

A system that receives and processes messages in a separate thread

Loopers are a core Haiku concept. Haiku embraces the multithreaded application model, where the functionality of the application is split up over different threads with a specific job. The best example of the use of a looper is in that every Window is its own Looper.

Haiku’s design consists of an Application with one or more Loopers. Each Looper then functions as an independent message queue, which receives messages from other parts of the application, or from external applications, and then dispatches these to Handlers. In this implementation of the API, any object may be a Handler, as long as it implements the Handler trait. A Looper has at least one Handler, which is referred to as the InitialState.

To create a new Looper, you call the Application::create_looper() method. This method takes as an argument a Box, which is a required instance of a type that that you define and use. The only requirement is that this State implements the Handler trait, so that it may receive and process messages. The State becomes the preferred Handler by default.

After creating a Looper, you can add additional Handlers to it, with the add_handler() method. After you add the Handler, the Looper takes ownership, this means that you can no longer manipulate the object yourself. It is possible to mark a Handler as the preferred Handler by using the add_preferred_handler() method. This will override any previously selected preferred Handler.

Once the Looper and its Handlers are set up, you can start the message queue by using the run() method. Calling this method will transfer ownership of the Looper object to the new Looper thread. Any interaction you may want with that thread, should be done through the messaging system.

A Looper will continue to run until the it gets a request to quit. This can be done by sending the B_QUIT_REQUESTED message. Additionally, a Looper will quit when the Application is quitting.

Implementations§

Source§

impl<A> Looper<A>
where A: Send + 'static,

Source

pub fn name(&self) -> &str

Get the name for this Looper

Source

pub fn get_messenger(&self) -> Messenger

Get a Messenger for this looper

This Messenger by default points to the preferred Handler.

Source

pub fn run(self) -> Result<()>

Start the message loop

When you use this method, the Looper ownership of the Looper object will be transferred to the Looper’s thread. The message processing will start, until the Looper is requested to quit.

Source

pub fn add_handler(&mut self, handler: Box<dyn Handler<A> + Send>)

Add a Handler to the message queue

The handler may be any object that implements the Handler trait. The object should be created on the heap (as a Box).

Source

pub fn add_preferred_handler(&mut self, handler: Box<dyn Handler<A> + Send>)

Add a preferred Handler to the message queue

Like the add_handler() method, this method takes ownership of any Handler. In addition, this method will also set the Handler as the preferred Handler of this Looper. This will overwrite the previously set preferred Handler.

Auto Trait Implementations§

§

impl<A> Freeze for Looper<A>

§

impl<A> !RefUnwindSafe for Looper<A>

§

impl<A> Send for Looper<A>

§

impl<A> !Sync for Looper<A>

§

impl<A> Unpin for Looper<A>

§

impl<A> !UnwindSafe for Looper<A>

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
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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.