Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"examples/busy_genserver_warning",
"examples/signal_test",
"examples/signal_test_threads",
"examples/chat_room",
]

[workspace.dependencies]
Expand Down
20 changes: 6 additions & 14 deletions concurrency/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
#[derive(Debug, thiserror::Error)]
pub enum ActorError {
#[error("Callback Error")]
Callback,
#[error("Initialization error")]
Initialization,
#[error("Server error")]
Server,
#[error("Unsupported Request on this Actor")]
RequestUnused,
#[error("Unsupported Message on this Actor")]
MessageUnused,
#[error("Actor stopped")]
ActorStopped,
#[error("Request to Actor timed out")]
RequestTimeout,
}

impl<T> From<spawned_rt::threads::mpsc::SendError<T>> for ActorError {
fn from(_value: spawned_rt::threads::mpsc::SendError<T>) -> Self {
Self::Server
Self::ActorStopped
}
}

impl<T> From<spawned_rt::tasks::mpsc::SendError<T>> for ActorError {
fn from(_value: spawned_rt::tasks::mpsc::SendError<T>) -> Self {
Self::Server
Self::ActorStopped
}
}

Expand All @@ -32,7 +24,7 @@ mod tests {

#[test]
fn test_error_into_std_error() {
let error: &dyn std::error::Error = &ActorError::Callback;
assert_eq!(error.to_string(), "Callback Error");
let error: &dyn std::error::Error = &ActorError::ActorStopped;
assert_eq!(error.to_string(), "Actor stopped");
}
}
4 changes: 1 addition & 3 deletions concurrency/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! spawned concurrency
//! Some basic traits and structs to implement concurrent code à-la-Erlang.
pub mod error;
pub mod messages;
pub mod message;
pub mod tasks;
pub mod threads;
52 changes: 52 additions & 0 deletions concurrency/src/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
pub trait Message: Send + 'static {
type Result: Send + 'static;
}

/// Declarative macro for defining message types.
///
/// Supports both unit structs and structs with fields, and they can be mixed
/// in a single invocation:
///
/// ```ignore
/// messages! {
/// GetCount -> u64;
/// Deposit { who: String, amount: i32 } -> Result<u64, BankError>;
/// Stop -> ()
/// }
/// ```
#[macro_export]
macro_rules! messages {
() => {};

// Base: unit message
($(#[$meta:meta])* $name:ident -> $result:ty) => {
$(#[$meta])*
#[derive(Debug)]
pub struct $name;
impl $crate::message::Message for $name {
type Result = $result;
}
};

// Base: struct message
($(#[$meta:meta])* $name:ident { $($field:ident : $ftype:ty),* $(,)? } -> $result:ty) => {
$(#[$meta])*
#[derive(Debug)]
pub struct $name { $(pub $field: $ftype,)* }
impl $crate::message::Message for $name {
type Result = $result;
}
};

// Recursive: unit message followed by more
($(#[$meta:meta])* $name:ident -> $result:ty; $($rest:tt)*) => {
$crate::messages!($(#[$meta])* $name -> $result);
$crate::messages!($($rest)*);
};

// Recursive: struct message followed by more
($(#[$meta:meta])* $name:ident { $($field:ident : $ftype:ty),* $(,)? } -> $result:ty; $($rest:tt)*) => {
$crate::messages!($(#[$meta])* $name { $($field : $ftype),* } -> $result);
$crate::messages!($($rest)*);
};
}
2 changes: 0 additions & 2 deletions concurrency/src/messages.rs

This file was deleted.

Loading
Loading