Skip to content
This repository was archived by the owner on Nov 9, 2019. It is now read-only.

Commit 0575b2a

Browse files
committed
Added documentation for aktoro-raw.
1 parent d8214e1 commit 0575b2a

File tree

10 files changed

+152
-4
lines changed

10 files changed

+152
-4
lines changed

aktoro-raw/src/action.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ pub trait Action: Send {
1313
pub trait ActionHandler<A: Send + 'static>: Actor {
1414
type Output: Send;
1515

16+
/// Handles the action, returning a result
17+
/// eventually containing the action's output.
1618
fn handle(&mut self, action: A, ctx: &mut Self::Context) -> Result<Self::Output, Self::Error>;
1719
}

aktoro-raw/src/actor.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,60 @@ pub trait Actor: Unpin + Send + Sized + 'static {
1010
type Error: StdError + Send;
1111

1212
#[allow(unused)]
13+
/// Called when the actor's context has been created
14+
/// but it hasn't been spawned by the runtime yet.
1315
fn starting(&mut self, ctx: &mut Self::Context) {}
1416

1517
#[allow(unused)]
18+
/// Called when the actor's has been spawned by the
19+
/// runtime and polled for the first time.
1620
fn started(&mut self, ctx: &mut Self::Context) {}
1721

1822
#[allow(unused)]
23+
/// Called when the actor has been asked to stop
24+
/// but has been left the option to cancel it.
1925
fn stopping(&mut self, ctx: &mut Self::Context) {}
2026

2127
#[allow(unused)]
28+
/// Called when the actor has accepted to stop or
29+
/// it has been asked to stop immediately.
2230
fn stopped(&mut self, ctx: &mut Self::Context) {}
2331
}
2432

2533
pub trait Status: PartialEq + Default + Clone + Send {
34+
/// Returns the status that an actor should have
35+
/// before [`Actor::starting`] is called.
36+
///
37+
/// [`Actor::starting`]: trait.Actor.html#method.starting
2638
fn starting() -> Self;
39+
40+
/// Returns the status that an actor should have
41+
/// before [`Actor::started`] is called.
42+
///
43+
/// [`Actor::started`]: trait.Actor.html#methood.started
2744
fn started() -> Self;
45+
46+
/// Returns the status that an actor should have
47+
/// before [`Actor::stopping`] is called.
48+
///
49+
/// ## Note
50+
///
51+
/// If after [`Actor::stopping`] is called, its
52+
/// status is still the same it will be stopped.
53+
///
54+
/// [`Actor::stopping`]: trait.Actor.html#method.stopping
2855
fn stopping() -> Self;
56+
57+
/// Returns the status that an actor should have
58+
/// before [`Actor::stopped`] is called.
59+
///
60+
/// [`Actor::stopped`]: trait.Actor.html#method.stopped
2961
fn stopped() -> Self;
62+
63+
/// Returns the status that an actor will have
64+
/// after [`Actor::stopped`] has been called.
65+
///
66+
/// [`Actor::stopped`]: trait.Actor.html#method.stopped
3067
fn dead() -> Self;
3168

3269
fn is_starting(&self) -> bool;

aktoro-raw/src/channel.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ use crate::actor::Actor;
77
use crate::message::Handler;
88
use crate::message::Message;
99

10+
/// The result returned by the [`Sender::try_send`]
11+
/// method.
12+
///
13+
/// `Ok` contains a future resolving with the result
14+
/// returned by the message handler.
15+
///
16+
/// [`Sender::try_send`]: trait.Sender.html#method.try_send
1017
pub type SenderRes<'s, O, E> = Result<BoxFuture<'s, Result<O, E>>, E>;
1118

1219
pub trait Sender<A: Actor>: Clone {
1320
type Receiver: Receiver<A>;
1421

1522
type Error: StdError + Send;
1623

24+
/// Tries to send a message to be handled by the
25+
/// actor.
1726
fn try_send<M>(&mut self, msg: M) -> SenderRes<A::Output, Self::Error>
1827
where
1928
A: Handler<M>,

aktoro-raw/src/context.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,69 @@ pub trait Context<A: Actor>: Unpin + Send + 'static + Stream<Item = Work<A>> {
1414
type Sender: Sender<A>;
1515
type Updater: Updater<A>;
1616

17+
/// Creates a new context for an actor.
1718
fn new() -> Self;
1819

20+
/// Emits an event that will be handled by the
21+
/// actor.
1922
fn emit<E>(&mut self, event: E)
2023
where
2124
A: EventHandler<E>,
2225
E: Send + 'static;
2326

27+
/// Gets the actor's current status.
2428
fn status(&self) -> &A::Status;
29+
30+
/// Sets the actor's current status, eventually
31+
/// making action to reflect the change (e.g. by
32+
/// stopping the actor).
2533
fn set_status(&mut self, status: A::Status);
34+
35+
/// Shares the actor's current status through
36+
/// the actor's update channel.
2637
fn update(&mut self) -> Result<(), <Self::Updater as Updater<A>>::Error>;
2738

39+
/// Gets the actor's action channel sender.
2840
fn controller(&self) -> &Self::Controller;
41+
42+
/// Gets the actor's message channel sender.
2943
fn sender(&self) -> &Self::Sender;
3044

45+
/// Tries to get a mutable reference to the
46+
/// actor's update channel sender.
3147
fn updated_ref(&mut self) -> Option<&mut <Self::Updater as Updater<A>>::Updated>;
48+
49+
/// Tries to get the actor's update channel
50+
/// sender.
3251
fn updated(&mut self) -> Option<<Self::Updater as Updater<A>>::Updated>;
3352

53+
/// Gets a mutable reference to the actor's
54+
/// action channel receiver.
3455
fn controlled(&mut self) -> &mut <Self::Controller as Controller<A>>::Controlled;
56+
57+
/// Gets a mutable reference to the actor's
58+
/// message channel receiver.
3559
fn receiver(&mut self) -> &mut <Self::Sender as Sender<A>>::Receiver;
60+
61+
/// Gets a mutable reference to the actors's
62+
/// update channel receiver.
3663
fn updater(&mut self) -> &mut Self::Updater;
3764
}
3865

3966
pub enum Work<A: Actor> {
67+
/// Contains an action that should be handled
68+
/// by the actor.
4069
Action(Box<dyn Action<Actor = A>>),
70+
71+
/// Contains an event that should be handled
72+
/// by the actor.
4173
Event(Box<dyn Event<Actor = A>>),
74+
75+
/// Contains a message that should be handled
76+
/// by the actor.
4277
Message(Box<dyn Message<Actor = A>>),
78+
79+
/// Indicates that the actor's status has
80+
/// changed.
4381
Update,
4482
}

aktoro-raw/src/control.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@ use crate::action::Action;
77
use crate::action::ActionHandler;
88
use crate::actor::Actor;
99

10+
/// The result returned by the [`Controller::try_send`]
11+
/// method.
12+
///
13+
/// `Ok` contains a future resolving with the result
14+
/// returned by the action handler.
15+
///
16+
/// [`Controller::try_send`]: trait.Controller.html#method.try_send
1017
pub type ControllerRes<'c, O, E> = Result<BoxFuture<'c, Result<O, E>>, E>;
1118

1219
pub trait Controller<A: Actor>: Clone {
1320
type Controlled: Controlled<A>;
1421

1522
type Error: StdError + Send;
1623

24+
/// Tries to send an action to be handled by the
25+
/// actor.
1726
fn try_send<D>(&mut self, action: D) -> ControllerRes<A::Output, Self::Error>
1827
where
1928
A: ActionHandler<D>,

aktoro-raw/src/event.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ pub trait Event: Send {
1111
}
1212

1313
pub trait EventHandler<E: Send + 'static>: Actor {
14+
/// Handles the event.
1415
fn handle(&mut self, event: E, ctx: &mut Self::Context) -> Result<(), Self::Error>;
1516
}

aktoro-raw/src/message.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ pub trait Message: Send {
1313
pub trait Handler<M: Send + 'static>: Actor {
1414
type Output: Send;
1515

16+
/// Handles the message, returning a result
17+
/// eventually containing the message's output.
1618
fn handle(&mut self, msg: M, ctx: &mut Self::Context) -> Result<Self::Output, Self::Error>;
1719
}

aktoro-raw/src/runtime.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,39 @@ use crate::actor::Actor;
55
use crate::spawned::Spawned;
66

77
pub trait Runtime {
8+
/// The future returned after calling the
9+
/// [`stop`] method. It will resolve after
10+
/// all the actors have been stopped.
11+
///
12+
/// [`stop`]: #method.stop
813
type Stop: Future<Output = Result<(), Self::Error>>;
14+
15+
/// The future returned after calling the
16+
/// [`wait`] method. It will resolve after
17+
/// all the actors have been stopped.
18+
///
19+
/// [`wait`]: #method.wait
920
type Wait: Future<Output = Result<(), Self::Error>>;
1021

1122
type Error: StdError;
1223

24+
/// Spawns a new actor on the runtime,
25+
/// returning [`Some(Spawned<A>)`] if it
26+
/// succeeded or [`None`] if it failed or
27+
/// if the actor stopped itself when
28+
/// [`Actor::starting`] was called.
29+
///
30+
/// [`Some(Spawned<A>)`]: sturct.Spawned.html
31+
/// [`Actor::starting`]: trait.Actor.html#method.starting
1332
fn spawn<A: Actor>(&mut self, actor: A) -> Option<Spawned<A>>;
1433

34+
/// Asks to all the actors managed by the
35+
/// runtime, to stop, returning a future
36+
/// resolving after all of them have been
37+
/// stopped.
1538
fn stop(self) -> Self::Stop;
1639

40+
/// Waits for all the actors to be stopped,
41+
/// returning a future waiting for it.
1742
fn wait(self) -> Self::Wait;
1843
}

aktoro-raw/src/spawned.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ pub struct Spawned<A: Actor> {
2929
}
3030

3131
impl<A: Actor> Spawned<A> {
32+
/// Creates a new `Spawned` struct from an actor's
33+
/// context.
34+
///
35+
/// ## Note
36+
///
37+
/// This should only be used by a runtime after
38+
/// an actor has finished starting.
3239
pub fn new(ctx: &mut A::Context) -> Spawned<A> {
3340
Spawned {
3441
sender: ctx.sender().clone(),
@@ -40,6 +47,10 @@ impl<A: Actor> Spawned<A> {
4047
}
4148
}
4249

50+
/// Tries to send a message over the actor's
51+
/// message channel, returning a future
52+
/// resolving with the result returned by the
53+
/// message handler.
4354
pub fn try_send_msg<M>(&mut self, msg: M) -> SenderRes<A::Output, SenderError<A>>
4455
where
4556
A: Handler<M>,
@@ -48,6 +59,10 @@ impl<A: Actor> Spawned<A> {
4859
self.sender.try_send(msg)
4960
}
5061

62+
/// Tries send an action over the actor's
63+
/// control channel, returning a future resolving
64+
/// with the result returned by the action
65+
/// handler.
5166
pub fn try_send_action<D>(&mut self, action: D) -> ControllerRes<A::Output, ControllerError<A>>
5267
where
5368
A: ActionHandler<D>,
@@ -56,21 +71,29 @@ impl<A: Actor> Spawned<A> {
5671
self.ctrler.try_send(action)
5772
}
5873

74+
/// Returns a reference to the actor's message
75+
/// channel sender.
5976
pub fn sender(&self) -> &Sender<A> {
6077
&self.sender
6178
}
6279

80+
/// Returns a reference to the actor's control
81+
/// channel sender.
6382
pub fn controller(&self) -> &Controller<A> {
6483
&self.ctrler
6584
}
6685

67-
pub fn updated(&mut self) -> Option<Updated<A>> {
68-
self.updted.take()
69-
}
70-
86+
/// Tries to return a mutable reference to the
87+
/// actor's update channel receiver.
7188
pub fn updated_ref(&mut self) -> Option<&mut Updated<A>> {
7289
self.updted.as_mut()
7390
}
91+
92+
/// Tries to return the actor's update channel
93+
/// receiver.
94+
pub fn updated(&mut self) -> Option<Updated<A>> {
95+
self.updted.take()
96+
}
7497
}
7598

7699
impl<A: Actor> Unpin for Spawned<A> {}

aktoro-raw/src/update.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ pub trait Updater<A: Actor> {
99

1010
type Error: StdError + Send;
1111

12+
/// Tries to send a status update over
13+
/// the actor's update channel.
1214
fn try_send(&mut self, status: A::Status) -> Result<(), Self::Error>;
1315
}
1416

0 commit comments

Comments
 (0)