dyn Sendable instead of impl Sendable
This commit is contained in:
parent
eeab6b8ddb
commit
fb9c4c97c5
20
Cargo.lock
generated
Normal file → Executable file
20
Cargo.lock
generated
Normal file → Executable file
@ -19,9 +19,9 @@ checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.83"
|
||||
version = "0.1.85"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd"
|
||||
checksum = "3f934833b4b7233644e5848f235df3f57ed8c80f1528a26c3dfa13d2147fa056"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -270,9 +270,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.167"
|
||||
version = "0.2.169"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc"
|
||||
checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
@ -549,9 +549,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.133"
|
||||
version = "1.0.135"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377"
|
||||
checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
@ -641,9 +641,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.41.1"
|
||||
version = "1.43.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33"
|
||||
checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"bytes",
|
||||
@ -669,9 +669,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.4.0"
|
||||
version = "2.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
|
||||
checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
6
Cargo.toml
Normal file → Executable file
6
Cargo.toml
Normal file → Executable file
@ -11,8 +11,8 @@ keywords = ["http", "server", "site", "async"]
|
||||
|
||||
[dependencies]
|
||||
urlencoding = "2.1.3"
|
||||
serde_json = "1.0.133"
|
||||
tokio = { version = "1.41.1", features = ["full"] }
|
||||
serde_json = "1.0.135"
|
||||
tokio = { version = "1.43.0", features = ["full"] }
|
||||
rusty_pool = "0.7.0"
|
||||
tokio-io-timeout = "1.2.0"
|
||||
threadpool = "1.8.1"
|
||||
@ -23,4 +23,4 @@ openssl = "0.10.68"
|
||||
tokio-openssl = "0.6.5"
|
||||
tokio-socks = "0.5.2"
|
||||
base64 = "0.22.1"
|
||||
async-trait = "0.1.83"
|
||||
async-trait = "0.1.85"
|
0
examples/request_meex.rs
Normal file → Executable file
0
examples/request_meex.rs
Normal file → Executable file
0
examples/simple_site.rs
Normal file → Executable file
0
examples/simple_site.rs
Normal file → Executable file
0
examples/small_site.rs
Normal file → Executable file
0
examples/small_site.rs
Normal file → Executable file
7
src/ezhttp/body.rs
Normal file → Executable file
7
src/ezhttp/body.rs
Normal file → Executable file
@ -2,7 +2,7 @@ use std::{collections::HashMap, path::PathBuf};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use serde_json::Value;
|
||||
use tokio::{fs, io::{AsyncReadExt, AsyncWriteExt}};
|
||||
use tokio::{fs, io::{AsyncReadExt, AsyncWrite, AsyncWriteExt}};
|
||||
|
||||
use crate::ezhttp::{split_bytes, split_bytes_once};
|
||||
|
||||
@ -154,7 +154,10 @@ impl Body {
|
||||
|
||||
#[async_trait]
|
||||
impl Sendable for Body {
|
||||
async fn send(&self, stream: &mut (impl AsyncWriteExt + Unpin + Send)) -> Result<(), HttpError> {
|
||||
async fn send(
|
||||
&self,
|
||||
stream: &mut (dyn AsyncWrite + Unpin + Send + Sync),
|
||||
) -> Result<(), HttpError> {
|
||||
stream.write_all(&self.as_bytes()).await.map_err(|_| HttpError::WriteHeadError)
|
||||
}
|
||||
}
|
||||
|
0
src/ezhttp/client/client.rs
Normal file → Executable file
0
src/ezhttp/client/client.rs
Normal file → Executable file
0
src/ezhttp/client/mod.rs
Normal file → Executable file
0
src/ezhttp/client/mod.rs
Normal file → Executable file
0
src/ezhttp/client/proxy.rs
Normal file → Executable file
0
src/ezhttp/client/proxy.rs
Normal file → Executable file
0
src/ezhttp/client/req_builder.rs
Normal file → Executable file
0
src/ezhttp/client/req_builder.rs
Normal file → Executable file
0
src/ezhttp/error.rs
Normal file → Executable file
0
src/ezhttp/error.rs
Normal file → Executable file
7
src/ezhttp/headers.rs
Normal file → Executable file
7
src/ezhttp/headers.rs
Normal file → Executable file
@ -4,7 +4,7 @@ use std::{
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
|
||||
use super::{error::HttpError, read_line_crlf, Sendable};
|
||||
|
||||
@ -131,7 +131,10 @@ impl Display for Headers {
|
||||
|
||||
#[async_trait]
|
||||
impl Sendable for Headers {
|
||||
async fn send(&self, stream: &mut (impl AsyncWriteExt + Unpin + Send)) -> Result<(), HttpError> {
|
||||
async fn send(
|
||||
&self,
|
||||
stream: &mut (dyn AsyncWrite + Unpin + Send + Sync),
|
||||
) -> Result<(), HttpError> {
|
||||
let mut head = String::new();
|
||||
for (k, v) in self.entries() {
|
||||
head.push_str(&k);
|
||||
|
9
src/ezhttp/mod.rs
Normal file → Executable file
9
src/ezhttp/mod.rs
Normal file → Executable file
@ -21,7 +21,7 @@ pub mod prelude {
|
||||
|
||||
use error::HttpError;
|
||||
use rand::Rng;
|
||||
use tokio::{io::{AsyncReadExt, AsyncWriteExt}, net::TcpStream};
|
||||
use tokio::{io::{AsyncReadExt, AsyncWrite}, net::TcpStream};
|
||||
use tokio_io_timeout::TimeoutStream;
|
||||
use async_trait::async_trait;
|
||||
|
||||
@ -108,8 +108,11 @@ async fn read_line_crlf(data: &mut (impl AsyncReadExt + Unpin)) -> Result<String
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait Sendable: Sized {
|
||||
async fn send(&self, stream: &mut (impl AsyncWriteExt + Unpin + Send)) -> Result<(), HttpError>;
|
||||
pub trait Sendable: Send + Sync {
|
||||
async fn send(
|
||||
&self,
|
||||
stream: &mut (dyn AsyncWrite + Unpin + Send + Sync),
|
||||
) -> Result<(), HttpError>;
|
||||
}
|
||||
|
||||
pub type Stream = TimeoutStream<TcpStream>;
|
8
src/ezhttp/request.rs
Normal file → Executable file
8
src/ezhttp/request.rs
Normal file → Executable file
@ -4,7 +4,7 @@ use std::{
|
||||
collections::HashMap, fmt::{Debug, Display}, net::SocketAddr, str::FromStr
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct URL {
|
||||
@ -196,8 +196,10 @@ impl HttpRequest {
|
||||
|
||||
#[async_trait]
|
||||
impl Sendable for HttpRequest {
|
||||
/// Write http request to stream
|
||||
async fn send(&self, stream: &mut (impl AsyncWriteExt + Unpin + Send)) -> Result<(), HttpError> {
|
||||
async fn send(
|
||||
&self,
|
||||
stream: &mut (dyn AsyncWrite + Unpin + Send + Sync),
|
||||
) -> Result<(), HttpError> {
|
||||
let mut head: String = String::new();
|
||||
head.push_str(&self.method);
|
||||
head.push_str(" ");
|
||||
|
8
src/ezhttp/response.rs
Normal file → Executable file
8
src/ezhttp/response.rs
Normal file → Executable file
@ -1,7 +1,7 @@
|
||||
use super::{body::{Body, Part}, gen_multipart_boundary, headers::Headers, read_line_crlf, HttpError, Sendable};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
pub mod status_code {
|
||||
@ -75,8 +75,10 @@ impl Default for HttpResponse {
|
||||
|
||||
#[async_trait]
|
||||
impl Sendable for HttpResponse {
|
||||
/// Write http response to stream
|
||||
async fn send(&self, stream: &mut (impl AsyncWriteExt + Unpin + Send)) -> Result<(), HttpError> {
|
||||
async fn send(
|
||||
&self,
|
||||
stream: &mut (dyn AsyncWrite + Unpin + Send + Sync),
|
||||
) -> Result<(), HttpError> {
|
||||
let mut head: String = String::new();
|
||||
head.push_str("HTTP/1.1 ");
|
||||
head.push_str(&self.status_code);
|
||||
|
2
src/ezhttp/server/handler.rs
Normal file → Executable file
2
src/ezhttp/server/handler.rs
Normal file → Executable file
@ -1,5 +1,3 @@
|
||||
use crate::Sendable;
|
||||
|
||||
use super::{
|
||||
HttpServer,
|
||||
super::{
|
||||
|
17
src/ezhttp/server/mod.rs
Normal file → Executable file
17
src/ezhttp/server/mod.rs
Normal file → Executable file
@ -2,11 +2,11 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::{
|
||||
boxed::Box,
|
||||
error::Error,
|
||||
future::Future,
|
||||
sync::Arc,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use threadpool::ThreadPool;
|
||||
use tokio::net::TcpListener;
|
||||
use tokio::runtime::Runtime;
|
||||
@ -24,19 +24,18 @@ pub mod starter;
|
||||
use handler::{handler_connection, Handler};
|
||||
|
||||
/// Async http server trait
|
||||
#[async_trait]
|
||||
pub trait HttpServer {
|
||||
fn on_start(&self, host: &str) -> impl Future<Output = ()> + Send;
|
||||
fn on_close(&self) -> impl Future<Output = ()> + Send;
|
||||
fn on_request(
|
||||
async fn on_start(&self, host: &str) -> ();
|
||||
async fn on_close(&self) -> ();
|
||||
async fn on_request(
|
||||
&self,
|
||||
req: &HttpRequest,
|
||||
) -> impl Future<Output = Option<impl Sendable + Send>> + Send;
|
||||
fn on_error(
|
||||
) -> Option<Box<dyn Sendable>>;
|
||||
async fn on_error(
|
||||
&self,
|
||||
_: HttpError
|
||||
) -> impl Future<Output = ()> + Send {
|
||||
async {}
|
||||
}
|
||||
) -> () {}
|
||||
}
|
||||
|
||||
async fn start_server_with_threadpool<T>(
|
||||
|
0
src/ezhttp/server/starter.rs
Normal file → Executable file
0
src/ezhttp/server/starter.rs
Normal file → Executable file
0
src/lib.rs
Normal file → Executable file
0
src/lib.rs
Normal file → Executable file
Loading…
Reference in New Issue
Block a user