Skip to content

Commit

Permalink
Reduce usage of Heph-rt in Heph tests
Browse files Browse the repository at this point in the history
To show that Heph works without Heph-rt.
  • Loading branch information
Thomasdezeeuw committed Apr 14, 2024
1 parent b89e886 commit e1371cf
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 220 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ log = { version = "0.4.21", default-features = false, features = [
getrandom = { version = "0.2.2", default-features = false, features = ["std"], optional = true }

[dev-dependencies]
# NOTE: these dependencies are only used for the examples.
heph-rt = { version = "0.5.0", path = "./rt", default-features = false, features = ["test"] }
std-logger = { version = "0.5.3", default-features = false, features = ["log-panic", "nightly"] }
# NOTE: the following two dependencies may only used by a limited number of examples.
heph-rt = { version = "0.5.0", path = "./rt", default-features = false }
std-logger = { version = "0.5.3", default-features = false }

[[test]]
name = "examples"
Expand Down
34 changes: 2 additions & 32 deletions src/actor/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ impl<M, RT> Context<M, RT> {
///
/// ```
/// use heph::actor;
/// use heph_rt::ThreadLocal;
///
/// async fn greeter_actor(mut ctx: actor::Context<String, ThreadLocal>) {
/// async fn greeter_actor(mut ctx: actor::Context<String>) {
/// if let Ok(name) = ctx.try_receive_next() {
/// println!("Hello: {name}");
/// } else {
Expand All @@ -68,43 +67,14 @@ impl<M, RT> Context<M, RT> {
///
/// ```
/// use heph::actor;
/// use heph_rt::ThreadLocal;
///
/// async fn print_actor(mut ctx: actor::Context<String, ThreadLocal>) {
/// async fn print_actor(mut ctx: actor::Context<String>) {
/// if let Ok(msg) = ctx.receive_next().await {
/// println!("Got a message: {msg}");
/// }
/// }
/// # _ = print_actor; // Silence dead code warnings.
/// ```
///
/// Same as the example above, but this actor will only wait for a limited
/// amount of time.
///
/// ```
/// use std::time::Duration;
///
/// use heph::actor;
/// use heph_rt::util::either;
/// use heph_rt::ThreadLocal;
/// use heph_rt::timer::Timer;
///
/// async fn print_actor(mut ctx: actor::Context<String, ThreadLocal>) {
/// // Create a timer, this will be ready once the timeout has
/// // passed.
/// let timeout = Timer::after(ctx.runtime().clone(), Duration::from_millis(100));
/// // Create a future to receive a message.
/// let msg_future = ctx.receive_next();
///
/// // Now let them race!
/// match either(msg_future, timeout).await {
/// Ok(Ok(msg)) => println!("Got a message: {msg}"),
/// Ok(Err(_)) => println!("No message"),
/// Err(_) => println!("Timed out receiving message"),
/// }
/// }
/// # _ = print_actor; // Silence dead code warnings.
/// ```
pub fn receive_next<'ctx>(&'ctx mut self) -> ReceiveMessage<'ctx, M> {
ReceiveMessage {
recv: self.inbox.recv(),
Expand Down
32 changes: 5 additions & 27 deletions src/actor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,13 @@ pub trait NewActor {
/// Here is an example of using an enum as message type.
///
/// ```
/// # #![feature(never_type)]
/// # use heph::actor::{self, actor_fn};
/// # use heph::from_message;
/// # use heph::supervisor::NoSupervisor;
/// # use heph_rt::spawn::ActorOptions;
/// # use heph_rt::{self as rt, Runtime, ThreadLocal};
/// #
/// # fn main() -> Result<(), rt::Error> {
/// # // Create and run the runtime.
/// # let mut runtime = Runtime::new()?;
/// # runtime.run_on_workers(|mut runtime_ref| -> Result<(), !> {
/// # // Spawn the actor.
/// # let actor_ref = runtime_ref.spawn_local(NoSupervisor, actor_fn(actor), (), ActorOptions::default());
/// #
/// # // Now we can use the reference to send the actor a message. We
/// # // don't have to use `Message` type we can just use `String`,
/// # // because `Message` implements `From<String>`.
/// # actor_ref.try_send("Hello world".to_owned()).unwrap();
/// # Ok(())
/// # })?;
/// # runtime.start()
/// # }
/// #
/// use heph::{actor, from_message};
///
/// /// The message type for the actor.
/// #[derive(Debug)]
/// # #[derive(Eq, PartialEq)]
/// # #[allow(dead_code)]
/// enum Message {
/// String(String),
/// # #[allow(dead_code)]
/// Number(usize),
/// }
///
Expand All @@ -173,12 +151,12 @@ pub trait NewActor {
/// from_message!(Message::Number(usize));
///
/// /// Our actor implementation that prints all messages it receives.
/// async fn actor(mut ctx: actor::Context<Message, ThreadLocal>) {
/// async fn actor(mut ctx: actor::Context<Message>) {
/// if let Ok(msg) = ctx.receive_next().await {
/// # assert_eq!(msg, Message::String("Hello world".to_owned()));
/// println!("received message: {msg:?}");
/// }
/// }
/// # _ = actor;
/// ```
type Message;

Expand Down
82 changes: 23 additions & 59 deletions src/actor_ref/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,14 @@
//! This example shows a simple actor that prints all the messages it receives.
//!
//! ```
//! # #![feature(never_type)]
//! use heph::actor::{self, actor_fn};
//! use heph::supervisor::NoSupervisor;
//! use heph_rt::spawn::ActorOptions;
//! use heph_rt::{self as rt, Runtime, ThreadLocal};
//!
//! fn main() -> Result<(), rt::Error> {
//! let mut runtime = Runtime::new()?;
//! runtime.run_on_workers(|mut runtime_ref| -> Result<(), !> {
//! // Spawn the actor.
//! let actor_ref = runtime_ref.spawn_local(NoSupervisor, actor_fn(actor), (), ActorOptions::default());
//!
//! // Now we can use the actor reference to send the actor a message.
//! actor_ref.try_send("Hello world".to_owned()).unwrap();
//!
//! Ok(())
//! })?;
//! runtime.start()
//! }
//! use heph::actor;
//!
//! /// Our actor.
//! async fn actor(mut ctx: actor::Context<String, ThreadLocal>) {
//! async fn actor(mut ctx: actor::Context<String>) {
//! if let Ok(msg) = ctx.receive_next().await {
//! println!("got message: {msg}");
//! }
//! }
//! # _ = actor;
//! ```
//!
//! [`send`]: ActorRef::send
Expand All @@ -82,29 +64,19 @@
//! # #![feature(never_type)]
//! use heph::actor::{self, actor_fn};
//! use heph::supervisor::NoSupervisor;
//! use heph_rt::spawn::ActorOptions;
//! use heph_rt::{self as rt, Runtime, ThreadLocal};
//!
//! fn main() -> Result<(), rt::Error> {
//! let mut runtime = Runtime::new()?;
//! runtime.run_on_workers(|mut runtime_ref| -> Result<(), !> {
//! let actor_ref = runtime_ref.spawn_local(NoSupervisor, actor_fn(actor), (), ActorOptions::default());
//! use heph::future::ActorFuture;
//!
//! // To create another actor reference we can simply clone the
//! // first one.
//! let second_actor_ref = actor_ref.clone();
//! let (future, actor_ref) = ActorFuture::new(NoSupervisor, actor_fn(actor), ()).unwrap();
//!
//! // Now we can use both references to send a message.
//! actor_ref.try_send("Hello world".to_owned()).unwrap();
//! second_actor_ref.try_send("Hallo wereld".to_owned()).unwrap(); // Hah! You learned a little Dutch!
//! // To create another actor reference we can simply clone the first one.
//! let second_actor_ref = actor_ref.clone();
//!
//! Ok(())
//! })?;
//! runtime.start()
//! }
//! // Now we can use both references to send a message.
//! actor_ref.try_send("Hello world".to_owned()).unwrap();
//! second_actor_ref.try_send("Hallo wereld".to_owned()).unwrap(); // Hah! You learned a little Dutch!
//!
//! /// Our actor.
//! async fn actor(mut ctx: actor::Context<String, ThreadLocal>) {
//! async fn actor(mut ctx: actor::Context<String>) {
//! if let Ok(msg) = ctx.receive_next().await {
//! println!("First message: {msg}");
//! }
Expand All @@ -113,6 +85,7 @@
//! println!("Second message: {msg}");
//! }
//! }
//! # _ = future;
//! ```
//!
//! # Joining an actor reference
Expand All @@ -126,42 +99,33 @@
//! was restarted. For that see the [`supervisor`] module.
//!
//! ```
//! # #![feature(never_type)]
//! use heph::ActorRef;
//! use heph::actor::{self, actor_fn};
//! use heph::future::ActorFuture;
//! use heph::supervisor::NoSupervisor;
//! use heph_rt::spawn::ActorOptions;
//! use heph_rt::{self as rt, Runtime, ThreadLocal};
//!
//! fn main() -> Result<(), rt::Error> {
//! let mut runtime = Runtime::new()?;
//! runtime.run_on_workers(|mut runtime_ref| -> Result<(), !> {
//! let actor_ref = runtime_ref.spawn_local(NoSupervisor, actor_fn(actor), (), ActorOptions::default());
//! let (actor_future, actor_ref) = ActorFuture::new(NoSupervisor, actor_fn(actor), ()).unwrap();
//!
//! // Send the actor a message to start.
//! actor_ref.try_send("Hello world".to_owned()).unwrap();
//! // Send the actor a message to start.
//! actor_ref.try_send("Hello world".to_owned()).unwrap();
//!
//! // Spawn our watchdog actor that watches the actor we spawned above.
//! runtime_ref.spawn_local(NoSupervisor, actor_fn(watchdog), actor_ref, ActorOptions::default());
//! // Create our watchdog actor that watches the actor we spawned above.
//! let (watchdog_future, _) = ActorFuture::new(NoSupervisor, actor_fn(watchdog), actor_ref).unwrap();
//!
//! Ok(())
//! })?;
//! runtime.start()
//! }
//!
//! /// Our actor.
//! async fn actor(mut ctx: actor::Context<String, ThreadLocal>) {
//! /// The actor we'll be watching.
//! async fn actor(mut ctx: actor::Context<String>) {
//! if let Ok(msg) = ctx.receive_next().await {
//! println!("First message: {msg}");
//! }
//! }
//!
//! /// Actor that watches another actor.
//! async fn watchdog<M>(_: actor::Context<!, ThreadLocal>, watch: ActorRef<M>) {
//! /// Actor that watches the actor above.
//! async fn watchdog<M>(_: actor::Context<()>, watch: ActorRef<M>) {
//! // Wait until the actor referenced by `watch` has completed.
//! watch.join().await;
//! println!("Actor has completed");
//! }
//! # _ = (actor_future, watchdog_future);
//! ```
//!
//! [`supervisor`]: crate::supervisor
Expand Down
50 changes: 8 additions & 42 deletions src/actor_ref/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
//! Using RPC to communicate with another actor.
//!
//! ```
//! # #![feature(never_type)]
//! #
//! use heph::actor;
//! use heph::actor_ref::{ActorRef, RpcMessage};
//! use heph_rt::{self as rt, ThreadLocal};
//!
//! /// Message type for [`counter`].
//! # #[allow(dead_code)]
//! struct Add(RpcMessage<usize, usize>);
//!
//! /// Required to support RPC.
Expand All @@ -38,7 +36,7 @@
//! }
//!
//! /// Receiving actor of the RPC.
//! async fn counter(mut ctx: actor::Context<Add, ThreadLocal>) {
//! async fn counter(mut ctx: actor::Context<Add>) {
//! // State of the counter.
//! let mut count: usize = 0;
//! // Receive a message like normal.
Expand All @@ -50,45 +48,29 @@
//! }
//!
//! /// Sending actor of the RPC.
//! async fn requester(_: actor::Context<!, ThreadLocal>, actor_ref: ActorRef<Add>) {
//! async fn requester(_: actor::Context<()>, actor_ref: ActorRef<Add>) {
//! // Make the procedure call.
//! let response = actor_ref.rpc(10).await;
//! # assert!(response.is_ok());
//! match response {
//! // We got a response.
//! Ok(count) => println!("Current count: {count}"),
//! // Actor failed to respond.
//! Err(err) => eprintln!("Counter didn't reply: {err}"),
//! }
//! }
//!
//! # fn main() -> Result<(), rt::Error> {
//! # use heph::actor::actor_fn;
//! # use heph::supervisor::NoSupervisor;
//! # use heph_rt::Runtime;
//! # use heph_rt::spawn::ActorOptions;
//! # let mut runtime = Runtime::new()?;
//! # runtime.run_on_workers(|mut runtime_ref| -> Result<(), !> {
//! # let actor_ref = runtime_ref.spawn_local(NoSupervisor, actor_fn(counter), (), ActorOptions::default());
//! # runtime_ref.spawn_local(NoSupervisor, actor_fn(requester), actor_ref, ActorOptions::default());
//! # Ok(())
//! # })?;
//! # runtime.start()
//! # }
//! # _ = (counter, requester);
//! ```
//!
//! Supporting multiple procedures within the same actor is possible by making
//! the message an `enum` as the example below shows. Furthermore this example
//! shows that synchronous actors are supported as well.
//!
//! ```
//! # #![feature(never_type)]
//! #
//! use heph::actor_ref::{ActorRef, RpcMessage};
//! use heph::{actor, from_message, sync};
//! use heph_rt::{self as rt, ThreadLocal};
//!
//! /// Message type for [`counter`].
//! # #[allow(dead_code)]
//! enum Message {
//! /// Increase the counter, returning the current state.
//! Add(RpcMessage<usize, usize>),
Expand All @@ -101,7 +83,7 @@
//! from_message!(Message::Get(()) -> usize);
//!
//! /// Receiving synchronous actor of the RPC.
//! fn counter<RT>(mut ctx: sync::Context<Message, RT>) {
//! fn counter(mut ctx: sync::Context<Message>) {
//! // State of the counter.
//! let mut count: usize = 0;
//!
Expand All @@ -122,7 +104,7 @@
//! }
//!
//! /// Sending actor of the RPC.
//! async fn requester(_: actor::Context<!, ThreadLocal>, actor_ref: ActorRef<Message>) {
//! async fn requester(_: actor::Context<()>, actor_ref: ActorRef<Message>) {
//! // Increase the counter by ten.
//! // NOTE: do handle the errors correctly in practice, this is just an
//! // example.
Expand All @@ -131,25 +113,9 @@
//!
//! // Retrieve the current count.
//! let count = actor_ref.rpc(()).await.unwrap();
//! # assert_eq!(count, 10);
//! println!("Current count {count}");
//! }
//!
//! # fn main() -> Result<(), rt::Error> {
//! # use heph::actor::actor_fn;
//! # use heph::supervisor::NoSupervisor;
//! # use heph_rt::Runtime;
//! # use heph_rt::spawn::{ActorOptions, SyncActorOptions};
//! #
//! # let mut runtime = Runtime::new()?;
//! # let options = SyncActorOptions::default();
//! # let actor_ref = runtime.spawn_sync_actor(NoSupervisor, actor_fn(counter), (), options)?;
//! # runtime.run_on_workers(move |mut runtime_ref| -> Result<(), !> {
//! # runtime_ref.spawn_local(NoSupervisor, actor_fn(requester), actor_ref, ActorOptions::default());
//! # Ok(())
//! # })?;
//! # runtime.start()
//! # }
//! # _ = (counter, requester);
//! ```
use std::error::Error;
Expand Down
Loading

0 comments on commit e1371cf

Please sign in to comment.