Skip to content

Commit

Permalink
rename .to_async() to .to()
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 committed Nov 21, 2019
1 parent 0b9e3d3 commit 8683ba8
Show file tree
Hide file tree
Showing 25 changed files with 232 additions and 396 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ actix-service = "1.0.0-alpha.1"
actix-utils = "0.5.0-alpha.1"
actix-router = "0.1.5"
actix-rt = "1.0.0-alpha.1"
actix-web-codegen = "0.1.2"
actix-web-codegen = "0.2.0-alpha.1"
actix-http = "0.3.0-alpha.1"
actix-server = "0.8.0-alpha.1"
actix-server-config = "0.3.0-alpha.1"
Expand Down
7 changes: 7 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 2.0.0

* Sync handlers has been removed. `.to_async()` methtod has been renamed to `.to()`

replace `fn` with `async fn` to convert sync handler to async


## 1.0.1

* Cors middleware has been moved to `actix-cors` crate
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Actix web is a simple, pragmatic and extremely fast web framework for Rust.
```rust
use actix_web::{web, App, HttpServer, Responder};

fn index(info: web::Path<(u32, String)>) -> impl Responder {
async fn index(info: web::Path<(u32, String)>) -> impl Responder {
format!("Hello {}! id:{}", info.1, info.0)
}

Expand Down
40 changes: 23 additions & 17 deletions actix-http/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
//! Http response
use std::cell::{Ref, RefMut};
use std::future::Future;
use std::io::Write;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{fmt, str};

use bytes::{BufMut, Bytes, BytesMut};
use futures::future::{ok, Ready};
use futures::Stream;
use futures::stream::Stream;
use serde::Serialize;
use serde_json;

Expand Down Expand Up @@ -280,15 +283,20 @@ impl<B: MessageBody> fmt::Debug for Response<B> {
}
}

// impl IntoFuture for Response {
// type Item = Response;
// type Error = Error;
// type Future = FutureResult<Response, Error>;
impl Future for Response {
type Output = Result<Response, Error>;

// fn into_future(self) -> Self::Future {
// ok(self)
// }
// }
fn poll(mut self: Pin<&mut Self>, _: &mut Context) -> Poll<Self::Output> {
Poll::Ready(Ok(Response {
head: std::mem::replace(
&mut self.head,
BoxedResponseHead::new(StatusCode::OK),
),
body: self.body.take_body(),
error: self.error.take(),
}))
}
}

pub struct CookieIter<'a> {
iter: header::GetAll<'a>,
Expand Down Expand Up @@ -757,15 +765,13 @@ impl<'a> From<&'a ResponseHead> for ResponseBuilder {
}
}

// impl IntoFuture for ResponseBuilder {
// type Item = Response;
// type Error = Error;
// type Future = FutureResult<Response, Error>;
impl Future for ResponseBuilder {
type Output = Result<Response, Error>;

// fn into_future(mut self) -> Self::Future {
// ok(self.finish())
// }
// }
fn poll(mut self: Pin<&mut Self>, _: &mut Context) -> Poll<Self::Output> {
Poll::Ready(Ok(self.finish()))
}
}

impl fmt::Debug for ResponseBuilder {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Expand Down
2 changes: 1 addition & 1 deletion actix-web-codegen/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum ResourceType {
impl ToTokens for ResourceType {
fn to_tokens(&self, stream: &mut TokenStream2) {
let ident = match self {
ResourceType::Async => "to_async",
ResourceType::Async => "to",
ResourceType::Sync => "to",
};
let ident = Ident::new(ident, Span::call_site());
Expand Down
12 changes: 5 additions & 7 deletions examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use actix_web::{
get, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer,
};
use actix_web::{get, middleware, web, App, HttpRequest, HttpResponse, HttpServer};

#[get("/resource1/{name}/index.html")]
fn index(req: HttpRequest, name: web::Path<String>) -> String {
async fn index(req: HttpRequest, name: web::Path<String>) -> String {
println!("REQ: {:?}", req);
format!("Hello: {}!\r\n", name)
}
Expand All @@ -14,7 +12,7 @@ async fn index_async(req: HttpRequest) -> &'static str {
}

#[get("/")]
fn no_params() -> &'static str {
async fn no_params() -> &'static str {
"Hello world!\r\n"
}

Expand All @@ -37,9 +35,9 @@ fn main() -> std::io::Result<()> {
.default_service(
web::route().to(|| HttpResponse::MethodNotAllowed()),
)
.route(web::get().to_async(index_async)),
.route(web::get().to(index_async)),
)
.service(web::resource("/test1.html").to(|| "Test\r\n"))
.service(web::resource("/test1.html").to(|| async { "Test\r\n" }))
})
.bind("127.0.0.1:8080")?
.workers(1)
Expand Down
16 changes: 8 additions & 8 deletions examples/uds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use actix_web::{
};

#[get("/resource1/{name}/index.html")]
fn index(req: HttpRequest, name: web::Path<String>) -> String {
async fn index(req: HttpRequest, name: web::Path<String>) -> String {
println!("REQ: {:?}", req);
format!("Hello: {}!\r\n", name)
}
Expand All @@ -14,11 +14,11 @@ async fn index_async(req: HttpRequest) -> Result<&'static str, Error> {
}

#[get("/")]
fn no_params() -> &'static str {
async fn no_params() -> &'static str {
"Hello world!\r\n"
}

#[cfg(feature = "uds")]
#[cfg(unix)]
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
env_logger::init();
Expand All @@ -27,7 +27,7 @@ fn main() -> std::io::Result<()> {
App::new()
.wrap(middleware::DefaultHeaders::new().header("X-Version", "0.2"))
.wrap(middleware::Compress::default())
// .wrap(middleware::Logger::default())
.wrap(middleware::Logger::default())
.service(index)
.service(no_params)
.service(
Expand All @@ -36,16 +36,16 @@ fn main() -> std::io::Result<()> {
middleware::DefaultHeaders::new().header("X-Version-R2", "0.3"),
)
.default_service(
web::route().to(|| ok(HttpResponse::MethodNotAllowed())),
web::route().to(|| HttpResponse::MethodNotAllowed()),
)
.route(web::get().to_async(index_async)),
.route(web::get().to(index_async)),
)
.service(web::resource("/test1.html").to(|| "Test\r\n"))
.service(web::resource("/test1.html").to(|| async { "Test\r\n" }))
})
.bind_uds("/Users/fafhrd91/uds-test")?
.workers(1)
.run()
}

#[cfg(not(feature = "uds"))]
#[cfg(not(unix))]
fn main() {}
12 changes: 6 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where
/// counter: Cell<usize>,
/// }
///
/// fn index(data: web::Data<MyData>) {
/// async fn index(data: web::Data<MyData>) {
/// data.counter.set(data.counter.get() + 1);
/// }
///
Expand Down Expand Up @@ -192,7 +192,7 @@ where
/// ```rust
/// use actix_web::{web, App, HttpResponse};
///
/// fn index(data: web::Path<(String, String)>) -> &'static str {
/// async fn index(data: web::Path<(String, String)>) -> &'static str {
/// "Welcome!"
/// }
///
Expand Down Expand Up @@ -247,7 +247,7 @@ where
/// ```rust
/// use actix_web::{web, App, HttpResponse};
///
/// fn index() -> &'static str {
/// async fn index() -> &'static str {
/// "Welcome!"
/// }
///
Expand Down Expand Up @@ -302,7 +302,7 @@ where
/// ```rust
/// use actix_web::{web, App, HttpRequest, HttpResponse, Result};
///
/// fn index(req: HttpRequest) -> Result<HttpResponse> {
/// async fn index(req: HttpRequest) -> Result<HttpResponse> {
/// let url = req.url_for("youtube", &["asdlkjqme"])?;
/// assert_eq!(url.as_str(), "https://youtube.com/watch/asdlkjqme");
/// Ok(HttpResponse::Ok().into())
Expand Down Expand Up @@ -346,7 +346,7 @@ where
/// use actix_web::{middleware, web, App};
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
///
/// fn index() -> &'static str {
/// async fn index() -> &'static str {
/// "Welcome!"
/// }
///
Expand Down Expand Up @@ -404,7 +404,7 @@ where
/// use actix_web::{web, App};
/// use actix_web::http::{header::CONTENT_TYPE, HeaderValue};
///
/// fn index() -> &'static str {
/// async fn index() -> &'static str {
/// "Welcome!"
/// }
///
Expand Down
2 changes: 1 addition & 1 deletion src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub(crate) trait DataFactory {
/// }
///
/// /// Use `Data<T>` extractor to access data in handler.
/// fn index(data: web::Data<Mutex<MyData>>) {
/// async fn index(data: web::Data<Mutex<MyData>>) {
/// let mut data = data.lock().unwrap();
/// data.counter += 1;
/// }
Expand Down
4 changes: 2 additions & 2 deletions src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub trait FromRequest: Sized {
/// }
///
/// /// extract `Thing` from request
/// fn index(supplied_thing: Option<Thing>) -> String {
/// async fn index(supplied_thing: Option<Thing>) -> String {
/// match supplied_thing {
/// // Puns not intended
/// Some(thing) => format!("Got something: {:?}", thing),
Expand Down Expand Up @@ -146,7 +146,7 @@ where
/// }
///
/// /// extract `Thing` from request
/// fn index(supplied_thing: Result<Thing>) -> String {
/// async fn index(supplied_thing: Result<Thing>) -> String {
/// match supplied_thing {
/// Ok(thing) => format!("Got thing: {:?}", thing),
/// Err(e) => format!("Error extracting thing: {}", e)
Expand Down
Loading

0 comments on commit 8683ba8

Please sign in to comment.