From 154cd3c5de553d1bbf05124aaf944f190574a9e8 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Fri, 1 Jun 2018 09:36:16 -0700 Subject: [PATCH] better actix mod re-exports --- src/client/connector.rs | 17 +++++----------- src/client/pipeline.rs | 6 ++---- src/httpmessage.rs | 19 +++++++++--------- src/lib.rs | 22 ++++++++++----------- src/middleware/session.rs | 27 +++++++++++++------------- src/server/mod.rs | 12 +++++------- src/server/srv.rs | 41 ++++++++++++++++++--------------------- src/server/worker.rs | 6 ++---- src/test.rs | 12 +++++------- src/ws/client.rs | 4 +--- src/ws/mod.rs | 5 +---- 11 files changed, 74 insertions(+), 97 deletions(-) diff --git a/src/client/connector.rs b/src/client/connector.rs index 82d932e47..4f142fde5 100644 --- a/src/client/connector.rs +++ b/src/client/connector.rs @@ -1,16 +1,12 @@ -extern crate actix; - use std::collections::{HashMap, VecDeque}; use std::net::Shutdown; use std::time::{Duration, Instant}; use std::{fmt, io, mem, time}; -use self::actix::actors::{Connect as ResolveConnect, Connector, ConnectorError}; -use self::actix::fut::WrapFuture; -use self::actix::registry::SystemService; -use self::actix::{ +use actix::resolver::{Connect as ResolveConnect, Connector, ConnectorError}; +use actix::{ fut, Actor, ActorFuture, ActorResponse, AsyncContext, Context, ContextFutureSpawner, - Handler, Message, Recipient, StreamHandler, Supervised, + Handler, Message, Recipient, StreamHandler, Supervised, SystemService, WrapFuture, }; use futures::sync::{mpsc, oneshot}; @@ -287,7 +283,6 @@ impl ClientConnector { /// /// ```rust /// # #![cfg(feature="alpn")] - /// # extern crate actix; /// # extern crate actix_web; /// # extern crate futures; /// # extern crate tokio; @@ -295,14 +290,12 @@ impl ClientConnector { /// # use std::io::Write; /// # use std::process; /// extern crate openssl; - /// use actix::prelude::*; - /// use actix_web::client::{Connect, ClientConnector}; + /// use actix_web::client::{ClientConnector, Connect}; /// - /// use openssl::ssl::{SslMethod, SslConnector}; + /// use openssl::ssl::{SslConnector, SslMethod}; /// /// fn main() { /// tokio::run(future::lazy(|| { - /// /// // Start `ClientConnector` with custom `SslConnector` /// let ssl_conn = SslConnector::builder(SslMethod::tls()).unwrap().build(); /// let conn = ClientConnector::with_connector(ssl_conn).start(); diff --git a/src/client/pipeline.rs b/src/client/pipeline.rs index 84593677d..c2caf83cf 100644 --- a/src/client/pipeline.rs +++ b/src/client/pipeline.rs @@ -1,5 +1,3 @@ -extern crate actix; - use bytes::{Bytes, BytesMut}; use futures::sync::oneshot; use futures::{Async, Future, Poll}; @@ -8,7 +6,7 @@ use std::time::{Duration, Instant}; use std::{io, mem}; use tokio_timer::Delay; -use self::actix::prelude::*; +use actix::{Addr, Request, SystemService}; use super::{ ClientBody, ClientBodyStream, ClientConnector, ClientConnectorError, ClientRequest, @@ -56,7 +54,7 @@ impl From for SendRequestError { enum State { New, - Connect(actix::dev::Request), + Connect(Request), Connection(Connection), Send(Box), None, diff --git a/src/httpmessage.rs b/src/httpmessage.rs index 2f0a9c99b..49d6a8a75 100644 --- a/src/httpmessage.rs +++ b/src/httpmessage.rs @@ -118,10 +118,11 @@ pub trait HttpMessage { /// # extern crate actix_web; /// # extern crate futures; /// # #[macro_use] extern crate serde_derive; + /// use actix_web::{ + /// AsyncResponder, FutureResponse, HttpMessage, HttpRequest, HttpResponse, + /// }; /// use bytes::Bytes; /// use futures::future::Future; - /// use actix_web::{HttpMessage, HttpRequest, HttpResponse, - /// FutureResponse, AsyncResponder}; /// /// fn index(mut req: HttpRequest) -> FutureResponse { /// req.body() // <- get Body future @@ -158,7 +159,7 @@ pub trait HttpMessage { /// # extern crate futures; /// # use futures::Future; /// # use std::collections::HashMap; - /// use actix_web::{HttpMessage, HttpRequest, HttpResponse, FutureResponse}; + /// use actix_web::{FutureResponse, HttpMessage, HttpRequest, HttpResponse}; /// /// fn index(mut req: HttpRequest) -> FutureResponse { /// Box::new( @@ -167,7 +168,8 @@ pub trait HttpMessage { /// .and_then(|params| { // <- url encoded parameters /// println!("==== BODY ==== {:?}", params); /// Ok(HttpResponse::Ok().into()) - /// })) + /// }), + /// ) /// } /// # fn main() {} /// ``` @@ -193,14 +195,14 @@ pub trait HttpMessage { /// # extern crate futures; /// # #[macro_use] extern crate serde_derive; /// use actix_web::*; - /// use futures::future::{Future, ok}; + /// use futures::future::{ok, Future}; /// /// #[derive(Deserialize, Debug)] /// struct MyObj { /// name: String, /// } /// - /// fn index(mut req: HttpRequest) -> Box> { + /// fn index(mut req: HttpRequest) -> Box> { /// req.json() // <- get JsonBody future /// .from_err() /// .and_then(|val: MyObj| { // <- deserialized value @@ -224,16 +226,15 @@ pub trait HttpMessage { /// ## Server example /// /// ```rust - /// # extern crate actix; /// # extern crate actix_web; /// # extern crate env_logger; /// # extern crate futures; /// # use std::str; /// # use actix_web::*; - /// # use actix::*; + /// # use actix_web::actix::*; /// # use futures::{Future, Stream}; /// # use futures::future::{ok, result, Either}; - /// fn index(mut req: HttpRequest) -> Box> { + /// fn index(mut req: HttpRequest) -> Box> { /// req.multipart().from_err() // <- get multipart stream for current request /// .and_then(|item| match item { // <- iterate over multipart items /// multipart::MultipartItem::Field(field) => { diff --git a/src/lib.rs b/src/lib.rs index 8d728f998..e8876a9c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,17 +6,17 @@ //! # use std::thread; //! //! fn index(info: Path<(String, u32)>) -> String { -//! format!("Hello {}! id:{}", info.0, info.1) +//! format!("Hello {}! id:{}", info.0, info.1) //! } //! //! fn main() { -//! # thread::spawn(|| { -//! server::new( -//! || App::new() -//! .resource("/{name}/{id}/index.html", |r| r.with(index))) -//! .bind("127.0.0.1:8080").unwrap() +//! //#### # thread::spawn(|| { +//! server::new(|| { +//! App::new().resource("/{name}/{id}/index.html", |r| r.with(index)) +//! }).bind("127.0.0.1:8080") +//! .unwrap() //! .run(); -//! # }); +//! //#### # }); //! } //! ``` //! @@ -198,13 +198,13 @@ pub use scope::Scope; pub mod actix { //! Re-exports [actix's](https://docs.rs/actix) prelude + pub use actix_inner::actors::resolver; + pub use actix_inner::actors::signal; + pub use actix_inner::fut; + pub use actix_inner::msgs; pub use actix_inner::prelude::*; } -#[doc(hidden)] -#[deprecated(since = "0.6.2", note = "please use `use actix_web::ws::WsWriter`")] -pub use ws::WsWriter; - #[cfg(feature = "openssl")] pub(crate) const HAS_OPENSSL: bool = true; #[cfg(not(feature = "openssl"))] diff --git a/src/middleware/session.rs b/src/middleware/session.rs index f80d1f11e..38be12355 100644 --- a/src/middleware/session.rs +++ b/src/middleware/session.rs @@ -32,8 +32,7 @@ //! session data. //! //! ```rust -//! # extern crate actix; -//! # extern crate actix_web; +//! //#### # extern crate actix_web; //! use actix_web::{server, App, HttpRequest, Result}; //! use actix_web::middleware::session::{RequestSession, SessionStorage, CookieSessionBackend}; //! @@ -59,7 +58,7 @@ //! ))) //! .bind("127.0.0.1:59880").unwrap() //! .start(); -//! # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0)); +//! //#### # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0)); //! }); //! } //! ``` @@ -88,13 +87,13 @@ use middleware::{Middleware, Response, Started}; /// The helper trait to obtain your session data from a request. /// /// ```rust -/// use actix_web::*; /// use actix_web::middleware::session::RequestSession; +/// use actix_web::*; /// /// fn index(mut req: HttpRequest) -> Result<&'static str> { /// // access session data /// if let Some(count) = req.session().get::("counter")? { -/// req.session().set("counter", count+1)?; +/// req.session().set("counter", count + 1)?; /// } else { /// req.session().set("counter", 1)?; /// } @@ -123,13 +122,13 @@ impl RequestSession for HttpRequest { /// method. `RequestSession` trait is implemented for `HttpRequest`. /// /// ```rust -/// use actix_web::*; /// use actix_web::middleware::session::RequestSession; +/// use actix_web::*; /// /// fn index(mut req: HttpRequest) -> Result<&'static str> { /// // access session data /// if let Some(count) = req.session().get::("counter")? { -/// req.session().set("counter", count+1)?; +/// req.session().set("counter", count + 1)?; /// } else { /// req.session().set("counter", 1)?; /// } @@ -200,7 +199,7 @@ impl Session { /// fn index(session: Session) -> Result<&'static str> { /// // access session data /// if let Some(count) = session.get::("counter")? { -/// session.set("counter", count+1)?; +/// session.set("counter", count + 1)?; /// } else { /// session.set("counter", 1)?; /// } @@ -230,15 +229,15 @@ unsafe impl Sync for SessionImplCell {} /// /// ```rust /// # extern crate actix_web; +/// use actix_web::middleware::session::{CookieSessionBackend, SessionStorage}; /// use actix_web::App; -/// use actix_web::middleware::session::{SessionStorage, CookieSessionBackend}; /// /// fn main() { -/// let app = App::new().middleware( -/// SessionStorage::new( // <- create session middleware -/// CookieSessionBackend::signed(&[0; 32]) // <- create cookie session backend -/// .secure(false)) -/// ); +/// let app = App::new().middleware(SessionStorage::new( +/// // <- create session middleware +/// CookieSessionBackend::signed(&[0; 32]) // <- create cookie session backend +/// .secure(false), +/// )); /// } /// ``` pub struct SessionStorage(T, PhantomData); diff --git a/src/server/mod.rs b/src/server/mod.rs index 32138f30b..44022931b 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,6 +1,4 @@ //! Http server -extern crate actix; - use std::net::Shutdown; use std::{io, time}; @@ -29,6 +27,7 @@ pub use self::srv::HttpServer; #[doc(hidden)] pub use self::helpers::write_content_length; +use actix::Message; use body::Binary; use error::Error; use header::ContentEncoding; @@ -43,9 +42,8 @@ pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536; /// This is shortcut for `server::HttpServer::new()` method. /// /// ```rust -/// # extern crate actix_web; -/// use actix_web::actix::*; -/// use actix_web::{server, App, HttpResponse}; +/// //#### # extern crate actix_web; +/// use actix_web::{actix, server, App, HttpResponse}; /// /// fn main() { /// actix::System::run(|| { @@ -56,7 +54,7 @@ pub(crate) const MAX_WRITE_BUFFER_SIZE: usize = 65_536; /// .bind("127.0.0.1:59090").unwrap() /// .start(); /// -/// # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0)); +/// //#### # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0)); /// }); /// } /// ``` @@ -116,7 +114,7 @@ pub struct StopServer { pub graceful: bool, } -impl actix::Message for StopServer { +impl Message for StopServer { type Result = Result<(), ()>; } diff --git a/src/server/srv.rs b/src/server/srv.rs index 5ea28d6c5..ba8c83e65 100644 --- a/src/server/srv.rs +++ b/src/server/srv.rs @@ -1,12 +1,13 @@ -extern crate actix; - use std::rc::Rc; use std::sync::{mpsc as sync_mpsc, Arc}; use std::time::Duration; use std::{io, net, thread}; -use self::actix::actors::signal; -use self::actix::prelude::*; +use actix::{ + fut, msgs, signal, Actor, ActorFuture, Addr, Arbiter, AsyncContext, Context, + ContextFutureSpawner, Handler, Response, StreamHandler, System, WrapFuture, +}; + use futures::sync::mpsc; use futures::{Future, Sink, Stream}; use mio; @@ -21,7 +22,7 @@ use native_tls::TlsAcceptor; #[cfg(feature = "alpn")] use openssl::ssl::{AlpnError, SslAcceptorBuilder}; -use super::channel::{WrapperStream}; +use super::channel::WrapperStream; use super::settings::{ServerSettings, WorkerSettings}; use super::worker::{Conn, SocketInfo, StopWorker, StreamHandlerType, Worker}; use super::{IntoHttpHandler, IoStream, KeepAlive}; @@ -408,20 +409,17 @@ impl HttpServer { /// /// ```rust /// extern crate actix_web; - /// extern crate actix; - /// use actix_web::{server, App, HttpResponse}; + /// use actix_web::{actix, server, App, HttpResponse}; /// /// fn main() { /// // Run actix system, this method actually starts all async processes /// actix::System::run(|| { - /// - /// server::new( - /// || App::new() - /// .resource("/", |r| r.h(|_| HttpResponse::Ok()))) - /// .bind("127.0.0.1:0").expect("Can not bind to 127.0.0.1:0") + /// server::new(|| App::new().resource("/", |r| r.h(|_| HttpResponse::Ok()))) + /// .bind("127.0.0.1:0") + /// .expect("Can not bind to 127.0.0.1:0") /// .start(); - /// # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0)); - /// }); + /// //#### # actix::Arbiter::system().do_send(actix::msgs::SystemExit(0)); + /// }); /// } /// ``` pub fn start(mut self) -> Addr { @@ -485,10 +483,9 @@ impl HttpServer { /// use actix_web::*; /// /// fn main() { - /// HttpServer::new( - /// || App::new() - /// .resource("/", |r| r.h(|_| HttpResponse::Ok()))) - /// .bind("127.0.0.1:0").expect("Can not bind to 127.0.0.1:0") + /// HttpServer::new(|| App::new().resource("/", |r| r.h(|_| HttpResponse::Ok()))) + /// .bind("127.0.0.1:0") + /// .expect("Can not bind to 127.0.0.1:0") /// .run(); /// } /// ``` @@ -723,7 +720,7 @@ impl Handler for HttpServer { } impl Handler for HttpServer { - type Result = actix::Response<(), ()>; + type Result = Response<(), ()>; fn handle(&mut self, msg: StopServer, ctx: &mut Context) -> Self::Result { // stop accept threads @@ -754,11 +751,11 @@ impl Handler for HttpServer { // we need to stop system if server was spawned if slf.exit { ctx.run_later(Duration::from_millis(300), |_, _| { - Arbiter::system().do_send(actix::msgs::SystemExit(0)) + Arbiter::system().do_send(msgs::SystemExit(0)) }); } } - actix::fut::ok(()) + fut::ok(()) }) .spawn(ctx); } @@ -769,7 +766,7 @@ impl Handler for HttpServer { // we need to stop system if server was spawned if self.exit { ctx.run_later(Duration::from_millis(300), |_, _| { - Arbiter::system().do_send(actix::msgs::SystemExit(0)) + Arbiter::system().do_send(msgs::SystemExit(0)) }); } Response::reply(Ok(())) diff --git a/src/server/worker.rs b/src/server/worker.rs index 636f859a4..5a3f88584 100644 --- a/src/server/worker.rs +++ b/src/server/worker.rs @@ -1,5 +1,3 @@ -extern crate actix; - use futures::sync::oneshot; use futures::Future; use net2::TcpStreamExt; @@ -23,8 +21,8 @@ use openssl::ssl::SslAcceptor; #[cfg(feature = "alpn")] use tokio_openssl::SslAcceptorExt; -use self::actix::msgs::StopArbiter; -use self::actix::*; +use actix::msgs::StopArbiter; +use actix::{Actor, Arbiter, AsyncContext, Context, Handler, Message, Response}; use server::channel::HttpChannel; use server::settings::WorkerSettings; diff --git a/src/test.rs b/src/test.rs index b022e35ed..ced447f58 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,13 +1,11 @@ //! Various helpers for Actix applications to use during testing. - -extern crate actix; - use std::rc::Rc; use std::str::FromStr; use std::sync::mpsc; use std::{net, thread}; -use self::actix::{msgs, Actor, Addr, Arbiter, System}; +use actix_inner::{msgs, Actor, Addr, Arbiter, System}; + use cookie::Cookie; use futures::Future; use http::header::HeaderName; @@ -409,11 +407,11 @@ impl Iterator for TestApp { /// /// fn main() { /// let resp = TestRequest::with_header("content-type", "text/plain") -/// .run(index).unwrap(); +/// .run(index) +/// .unwrap(); /// assert_eq!(resp.status(), StatusCode::OK); /// -/// let resp = TestRequest::default() -/// .run(index).unwrap(); +/// let resp = TestRequest::default().run(index).unwrap(); /// assert_eq!(resp.status(), StatusCode::BAD_REQUEST); /// } /// ``` diff --git a/src/ws/client.rs b/src/ws/client.rs index ac77a28fb..f5541beb9 100644 --- a/src/ws/client.rs +++ b/src/ws/client.rs @@ -1,6 +1,4 @@ //! Http client request -extern crate actix; - use std::cell::UnsafeCell; use std::rc::Rc; use std::time::Duration; @@ -16,7 +14,7 @@ use http::{Error as HttpError, HttpTryFrom, StatusCode}; use rand; use sha1::Sha1; -use self::actix::prelude::*; +use actix::{Addr, SystemService}; use body::Binary; use error::{Error, UrlParseError}; diff --git a/src/ws/mod.rs b/src/ws/mod.rs index 61ec7df91..4c079dad9 100644 --- a/src/ws/mod.rs +++ b/src/ws/mod.rs @@ -25,7 +25,6 @@ //! //! // Handler for ws::Message messages //! impl StreamHandler for Ws { -//! //! fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { //! match msg { //! ws::Message::Ping(msg) => ctx.pong(&msg), @@ -42,13 +41,11 @@ //! # .finish(); //! # } //! ``` -extern crate actix; - use bytes::Bytes; use futures::{Async, Poll, Stream}; use http::{header, Method, StatusCode}; -use self::actix::{Actor, AsyncContext, StreamHandler}; +use super::actix::{Actor, AsyncContext, StreamHandler}; use body::Binary; use error::{Error, PayloadError, ResponseError};