3 releases (1 unstable)

1.1.0-alpha May 10, 2020
1.0.0-alpha May 3, 2020
0.1.0 Jun 26, 2019

#1694 in Asynchronous

MIT/Apache

30KB
787 lines

Bottle

Documentation Crate informations Repository

An actor model implementation for Rust.

Basic usage

#![feature(arbitrary_self_types)]
use bottle::{Output, Receiver, Remote, Handler, EventQueue};

pub struct Foo {
	//
}

pub enum Event {
	Ping(Remote<Foo>),
	Pong
}

impl bottle::Event for Event {
	type Response = ();
}

impl Handler<Event> for Foo {
	fn handle(self: Receiver<Self>, event: Event) -> Output<()> {
		match event {
			Event::Ping(remote) => async move {
				println!("ping");
				remote.send(Event::Pong).await
			}.into(),
			Event::Pong => {
				println!("pong");
				Output::Now(())
			}
		}
	}
}

#[async_std::main]
async fn main() {
	let queue = EventQueue::new();

	let a = Remote::new(queue.reference(), Foo { });
	let b = Remote::new(queue.reference(), Foo { });

	std::thread::spawn(move || {
		async_std::task::block_on(queue.process())
	});

	a.send(Event::Ping(b)).await;
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.4–2MB
~31K SLoC