dyn Sendable instead of impl Sendable

This commit is contained in:
MeexReay 2025-01-14 15:46:27 +03:00
parent eeab6b8ddb
commit fb9c4c97c5
21 changed files with 47 additions and 37 deletions

20
Cargo.lock generated Normal file → Executable file
View File

@ -19,9 +19,9 @@ checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627"
[[package]] [[package]]
name = "async-trait" name = "async-trait"
version = "0.1.83" version = "0.1.85"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" checksum = "3f934833b4b7233644e5848f235df3f57ed8c80f1528a26c3dfa13d2147fa056"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -270,9 +270,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.167" version = "0.2.169"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a"
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -549,9 +549,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.133" version = "1.0.135"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7fceb2473b9166b2294ef05efcb65a3db80803f0b03ef86a5fc88a2b85ee377" checksum = "2b0d7ba2887406110130a978386c4e1befb98c674b4fba677954e4db976630d9"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr", "memchr",
@ -641,9 +641,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio" name = "tokio"
version = "1.41.1" version = "1.43.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" checksum = "3d61fa4ffa3de412bfea335c6ecff681de2b609ba3c77ef3e00e521813a9ed9e"
dependencies = [ dependencies = [
"backtrace", "backtrace",
"bytes", "bytes",
@ -669,9 +669,9 @@ dependencies = [
[[package]] [[package]]
name = "tokio-macros" name = "tokio-macros"
version = "2.4.0" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

6
Cargo.toml Normal file → Executable file
View File

@ -11,8 +11,8 @@ keywords = ["http", "server", "site", "async"]
[dependencies] [dependencies]
urlencoding = "2.1.3" urlencoding = "2.1.3"
serde_json = "1.0.133" serde_json = "1.0.135"
tokio = { version = "1.41.1", features = ["full"] } tokio = { version = "1.43.0", features = ["full"] }
rusty_pool = "0.7.0" rusty_pool = "0.7.0"
tokio-io-timeout = "1.2.0" tokio-io-timeout = "1.2.0"
threadpool = "1.8.1" threadpool = "1.8.1"
@ -23,4 +23,4 @@ openssl = "0.10.68"
tokio-openssl = "0.6.5" tokio-openssl = "0.6.5"
tokio-socks = "0.5.2" tokio-socks = "0.5.2"
base64 = "0.22.1" base64 = "0.22.1"
async-trait = "0.1.83" async-trait = "0.1.85"

0
LICENSE Normal file → Executable file
View File

0
README.md Normal file → Executable file
View File

0
examples/request_meex.rs Normal file → Executable file
View File

0
examples/simple_site.rs Normal file → Executable file
View File

0
examples/small_site.rs Normal file → Executable file
View File

7
src/ezhttp/body.rs Normal file → Executable file
View File

@ -2,7 +2,7 @@ use std::{collections::HashMap, path::PathBuf};
use async_trait::async_trait; use async_trait::async_trait;
use serde_json::Value; 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}; use crate::ezhttp::{split_bytes, split_bytes_once};
@ -154,7 +154,10 @@ impl Body {
#[async_trait] #[async_trait]
impl Sendable for Body { 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) stream.write_all(&self.as_bytes()).await.map_err(|_| HttpError::WriteHeadError)
} }
} }

0
src/ezhttp/client/client.rs Normal file → Executable file
View File

0
src/ezhttp/client/mod.rs Normal file → Executable file
View File

0
src/ezhttp/client/proxy.rs Normal file → Executable file
View File

0
src/ezhttp/client/req_builder.rs Normal file → Executable file
View File

0
src/ezhttp/error.rs Normal file → Executable file
View File

7
src/ezhttp/headers.rs Normal file → Executable file
View File

@ -4,7 +4,7 @@ use std::{
}; };
use async_trait::async_trait; use async_trait::async_trait;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
use super::{error::HttpError, read_line_crlf, Sendable}; use super::{error::HttpError, read_line_crlf, Sendable};
@ -131,7 +131,10 @@ impl Display for Headers {
#[async_trait] #[async_trait]
impl Sendable for Headers { 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(); let mut head = String::new();
for (k, v) in self.entries() { for (k, v) in self.entries() {
head.push_str(&k); head.push_str(&k);

9
src/ezhttp/mod.rs Normal file → Executable file
View File

@ -21,7 +21,7 @@ pub mod prelude {
use error::HttpError; use error::HttpError;
use rand::Rng; use rand::Rng;
use tokio::{io::{AsyncReadExt, AsyncWriteExt}, net::TcpStream}; use tokio::{io::{AsyncReadExt, AsyncWrite}, net::TcpStream};
use tokio_io_timeout::TimeoutStream; use tokio_io_timeout::TimeoutStream;
use async_trait::async_trait; use async_trait::async_trait;
@ -108,8 +108,11 @@ async fn read_line_crlf(data: &mut (impl AsyncReadExt + Unpin)) -> Result<String
} }
#[async_trait] #[async_trait]
pub trait Sendable: Sized { pub trait Sendable: Send + Sync {
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>;
} }
pub type Stream = TimeoutStream<TcpStream>; pub type Stream = TimeoutStream<TcpStream>;

8
src/ezhttp/request.rs Normal file → Executable file
View File

@ -4,7 +4,7 @@ use std::{
collections::HashMap, fmt::{Debug, Display}, net::SocketAddr, str::FromStr collections::HashMap, fmt::{Debug, Display}, net::SocketAddr, str::FromStr
}; };
use async_trait::async_trait; use async_trait::async_trait;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct URL { pub struct URL {
@ -196,8 +196,10 @@ impl HttpRequest {
#[async_trait] #[async_trait]
impl Sendable for HttpRequest { impl Sendable for HttpRequest {
/// Write http request to stream async fn send(
async fn send(&self, stream: &mut (impl AsyncWriteExt + Unpin + Send)) -> Result<(), HttpError> { &self,
stream: &mut (dyn AsyncWrite + Unpin + Send + Sync),
) -> Result<(), HttpError> {
let mut head: String = String::new(); let mut head: String = String::new();
head.push_str(&self.method); head.push_str(&self.method);
head.push_str(" "); head.push_str(" ");

8
src/ezhttp/response.rs Normal file → Executable file
View File

@ -1,7 +1,7 @@
use super::{body::{Body, Part}, gen_multipart_boundary, headers::Headers, read_line_crlf, HttpError, Sendable}; use super::{body::{Body, Part}, gen_multipart_boundary, headers::Headers, read_line_crlf, HttpError, Sendable};
use async_trait::async_trait; use async_trait::async_trait;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWrite, AsyncWriteExt};
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
pub mod status_code { pub mod status_code {
@ -75,8 +75,10 @@ impl Default for HttpResponse {
#[async_trait] #[async_trait]
impl Sendable for HttpResponse { impl Sendable for HttpResponse {
/// Write http response to stream async fn send(
async fn send(&self, stream: &mut (impl AsyncWriteExt + Unpin + Send)) -> Result<(), HttpError> { &self,
stream: &mut (dyn AsyncWrite + Unpin + Send + Sync),
) -> Result<(), HttpError> {
let mut head: String = String::new(); let mut head: String = String::new();
head.push_str("HTTP/1.1 "); head.push_str("HTTP/1.1 ");
head.push_str(&self.status_code); head.push_str(&self.status_code);

2
src/ezhttp/server/handler.rs Normal file → Executable file
View File

@ -1,5 +1,3 @@
use crate::Sendable;
use super::{ use super::{
HttpServer, HttpServer,
super::{ super::{

17
src/ezhttp/server/mod.rs Normal file → Executable file
View File

@ -2,11 +2,11 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::{ use std::{
boxed::Box, boxed::Box,
error::Error, error::Error,
future::Future,
sync::Arc, sync::Arc,
time::Duration, time::Duration,
}; };
use async_trait::async_trait;
use threadpool::ThreadPool; use threadpool::ThreadPool;
use tokio::net::TcpListener; use tokio::net::TcpListener;
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
@ -24,19 +24,18 @@ pub mod starter;
use handler::{handler_connection, Handler}; use handler::{handler_connection, Handler};
/// Async http server trait /// Async http server trait
#[async_trait]
pub trait HttpServer { pub trait HttpServer {
fn on_start(&self, host: &str) -> impl Future<Output = ()> + Send; async fn on_start(&self, host: &str) -> ();
fn on_close(&self) -> impl Future<Output = ()> + Send; async fn on_close(&self) -> ();
fn on_request( async fn on_request(
&self, &self,
req: &HttpRequest, req: &HttpRequest,
) -> impl Future<Output = Option<impl Sendable + Send>> + Send; ) -> Option<Box<dyn Sendable>>;
fn on_error( async fn on_error(
&self, &self,
_: HttpError _: HttpError
) -> impl Future<Output = ()> + Send { ) -> () {}
async {}
}
} }
async fn start_server_with_threadpool<T>( async fn start_server_with_threadpool<T>(

0
src/ezhttp/server/starter.rs Normal file → Executable file
View File

0
src/lib.rs Normal file → Executable file
View File