Remove dependency on futures-util, thiserror

This commit is contained in:
Aode (Lion) 2022-03-09 23:06:38 -06:00
parent 3d6f491dd8
commit c40608eceb
3 changed files with 44 additions and 15 deletions

View file

@ -1,7 +1,7 @@
[package]
name = "actix-ws"
description = "Websockets for the Actix runtime, without Actors"
version = "0.2.2"
version = "0.2.3"
authors = ["asonix <asonix@asonix.dog>"]
readme = "README.md"
repository = "https://git.asonix.dog/asonix/actix-actorless-websockets"
@ -13,11 +13,9 @@ edition = "2018"
members = ["examples/chat"]
[dependencies]
actix-codec = "0.5.0"
actix-http = { version = "3.0", default-features = false, features = ["ws"] }
actix-web = { version = "4.0", default-features = false }
bytes = "1.0"
futures-util = "0.3"
futures-core = "0.3"
pin-project-lite = "0.2"
thiserror = "1.0"
tokio = { version = "1", features = ["sync", "io-util"] }
tokio-util = { version = "0.7", features = ["codec"] }

View file

@ -1,18 +1,21 @@
use actix_codec::{Decoder, Encoder};
use actix_http::{
ws::{Codec, Frame, Message, ProtocolError},
Payload,
};
use actix_web::Error;
use bytes::{Bytes, BytesMut};
use futures_util::stream::{Stream, StreamExt};
use actix_web::{
web::{Bytes, BytesMut},
Error,
};
use futures_core::stream::Stream;
use std::{
collections::VecDeque,
future::Future,
io,
pin::Pin,
task::{Context, Poll},
};
use tokio::sync::mpsc::Receiver;
use tokio_util::codec::{Decoder, Encoder};
pin_project_lite::pin_project! {
/// A response body for Websocket HTTP Requests
@ -72,9 +75,30 @@ impl MessageStream {
/// // handle message
/// }
/// ```
#[allow(clippy::should_implement_trait)]
pub async fn next(&'_ mut self) -> Option<Result<Message, ProtocolError>> {
StreamExt::next(self).await
pub async fn recv(&mut self) -> Option<Result<Message, ProtocolError>> {
poll_fn(|cx| Pin::new(&mut *self).poll_next(cx)).await
}
}
struct PollFn<F>(F);
impl<F> Unpin for PollFn<F> {}
fn poll_fn<F, T>(f: F) -> PollFn<F>
where
F: FnMut(&mut Context<'_>) -> Poll<T>,
{
PollFn(f)
}
impl<F, T> Future for PollFn<F>
where
F: FnMut(&mut Context<'_>) -> Poll<T>,
{
type Output = T;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
(&mut self.0)(cx)
}
}

View file

@ -1,5 +1,5 @@
use actix_http::ws::{CloseReason, Message};
use bytes::Bytes;
use actix_web::web::Bytes;
use std::sync::{
atomic::{AtomicBool, Ordering},
Arc,
@ -16,10 +16,17 @@ pub struct Session {
}
/// The error representing a closed websocket session
#[derive(Debug, thiserror::Error)]
#[error("Session is closed")]
#[derive(Debug)]
pub struct Closed;
impl std::fmt::Display for Closed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Session is closed")
}
}
impl std::error::Error for Closed {}
impl Session {
pub(super) fn new(inner: Sender<Message>) -> Self {
Session {