1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//! Actix is an actor framework.
//!
//! [Actors](Actor) are objects which encapsulate state and behavior, they communicate exclusively
//! by exchanging messages. Actix actors are implemented on top of [Tokio](https://tokio.rs).
//!
//! Multiple actors can run in same thread. Actors can run in multiple threads using the [`Arbiter`]
//! API. Actors exchange typed messages.
//!
//! ## Features
//!
//! - Async or sync actors
//! - Actor communication in a local/thread context
//! - Using Futures for asynchronous message handling
//! - Actor supervision
//! - Typed messages (no [`Any`](std::any::Any) type) and generic messages are allowed
//! - Runs on stable Rust 1.68+
//!
//! ## Other Documentation
//!
//! - [User Guide](https://actix.rs/docs/actix)
//! - [Community Chat on Discord](https://discord.gg/NWpN5mmg3x)

#![deny(rust_2018_idioms, nonstandard_style, future_incompatible)]
#![doc(html_logo_url = "https://actix.rs/img/logo.png")]
#![doc(html_favicon_url = "https://actix.rs/favicon.ico")]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]

#[cfg(doctest)]
doc_comment::doctest!("../README.md");

mod actor;
mod address;
mod context;
mod context_impl;
mod context_items;
mod handler;
mod mailbox;
mod stream;
mod supervisor;

pub mod actors;
pub mod clock;
pub mod fut;
pub mod io;
pub mod registry;
pub mod sync;
pub mod utils;

#[cfg(feature = "macros")]
pub use actix_derive::{main, test, Message, MessageResponse};
pub use actix_rt::{spawn, Arbiter, ArbiterHandle, System, SystemRunner};

#[doc(hidden)]
pub mod __private {
    #[cfg(feature = "macros")]
    pub use actix_macros::{main, test};
}

#[doc(hidden)]
pub use crate::context::ContextFutureSpawner;
pub use crate::{
    actor::{Actor, ActorContext, ActorState, AsyncContext, Running, SpawnHandle, Supervised},
    address::{Addr, MailboxError, Recipient, WeakAddr, WeakRecipient},
    context::Context,
    fut::{
        ActorFuture, ActorFutureExt, ActorStream, ActorStreamExt, ActorTryFuture,
        ActorTryFutureExt, WrapFuture, WrapStream,
    },
    handler::{
        ActorResponse, AtomicResponse, Handler, Message, MessageResult, Response,
        ResponseActFuture, ResponseFuture,
    },
    registry::{ArbiterService, Registry, SystemRegistry, SystemService},
    stream::StreamHandler,
    supervisor::Supervisor,
    sync::{SyncArbiter, SyncContext},
};

pub mod prelude {
    //! The `actix` prelude.
    //!
    //! The purpose of this module is to alleviate imports of many common actix
    //! traits by adding a glob import to the top of actix heavy modules:
    //!
    //! ```
    //! # #![allow(unused_imports)]
    //! use actix::prelude::*;
    //! ```

    #[doc(hidden)]
    #[cfg(feature = "macros")]
    pub use actix_derive::{Message, MessageResponse};
    pub use actix_rt::{Arbiter, ArbiterHandle, System, SystemRunner};
    pub use futures_core::stream::Stream;

    #[allow(deprecated)]
    pub use crate::utils::Condition;
    pub use crate::{
        actor::{Actor, ActorContext, ActorState, AsyncContext, Running, SpawnHandle, Supervised},
        actors,
        address::{Addr, MailboxError, Recipient, RecipientRequest, Request, SendError},
        context::{Context, ContextFutureSpawner},
        dev, fut,
        fut::{
            ActorFuture, ActorFutureExt, ActorStream, ActorStreamExt, ActorTryFuture,
            ActorTryFutureExt, WrapFuture, WrapStream,
        },
        handler::{
            ActorResponse, AtomicResponse, Handler, Message, MessageResult, Response,
            ResponseActFuture, ResponseFuture,
        },
        io,
        registry::{ArbiterService, SystemService},
        stream::StreamHandler,
        supervisor::Supervisor,
        sync::{SyncArbiter, SyncContext},
        utils::{IntervalFunc, TimerFunc},
    };
}

pub mod dev {
    //! The `actix` prelude for library developers.
    //!
    //! The purpose of this module is to alleviate imports of many common actix
    //! traits by adding a glob import to the top of actix heavy modules:
    //!
    //! ```
    //! # #![allow(unused_imports)]
    //! use actix::dev::*;
    //! ```

    pub use crate::{
        address::{Envelope, EnvelopeProxy, RecipientRequest, Request, ToEnvelope},
        prelude::*,
    };
    pub mod channel {
        pub use crate::address::channel::{channel, AddressReceiver, AddressSender};
    }
    pub use crate::{
        context_impl::{AsyncContextParts, ContextFut, ContextParts},
        handler::{MessageResponse, OneshotSender},
        mailbox::Mailbox,
        registry::{Registry, SystemRegistry},
    };
}

/// Starts the system and executes the supplied future.
///
/// This function does the following:
///
/// * Creates and starts the actix system with default configuration.
/// * Spawns the given future onto the current arbiter.
/// * Blocks the current thread until the system shuts down.
///
/// The `run` function returns when the `System::current().stop()`
/// method gets called.
///
/// # Examples
///
/// ```
/// use std::time::{Duration, Instant};
/// use actix_rt::time::sleep;
///
/// fn main() {
///   actix::run(async move {
///       sleep(Duration::from_millis(100)).await;
///       actix::System::current().stop();
///   });
/// }
/// ```
///
/// # Panics
///
/// This function panics if the actix system is already running.
#[allow(clippy::unit_arg, clippy::needless_doctest_main)]
pub fn run<R>(f: R) -> std::io::Result<()>
where
    R: std::future::Future<Output = ()> + 'static,
{
    Ok(actix_rt::System::new().block_on(f))
}