rename `ws::ContinuationItem`

This commit is contained in:
Rob Ede 2022-01-24 12:30:31 +00:00
parent 5454699bab
commit 09dcde2612
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
4 changed files with 43 additions and 25 deletions

View File

@ -1,6 +1,9 @@
# Changes # Changes
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
- Rename `ws::{Item => ContinuationItem}`. [#????]
[#????]: https://github.com/actix/actix-web/pull/????
## 3.0.0-beta.19 - 2022-01-21 ## 3.0.0-beta.19 - 2022-01-21

View File

@ -17,7 +17,7 @@ pub enum Message {
Binary(Bytes), Binary(Bytes),
/// Continuation. /// Continuation.
Continuation(Item), Continuation(ContinuationItem),
/// Ping message. /// Ping message.
Ping(Bytes), Ping(Bytes),
@ -42,7 +42,7 @@ pub enum Frame {
Binary(Bytes), Binary(Bytes),
/// Continuation. /// Continuation.
Continuation(Item), Continuation(ContinuationItem),
/// Ping message. /// Ping message.
Ping(Bytes), Ping(Bytes),
@ -56,13 +56,17 @@ pub enum Frame {
/// A WebSocket continuation item. /// A WebSocket continuation item.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Item { pub enum ContinuationItem {
FirstText(Bytes), FirstText(Bytes),
FirstBinary(Bytes), FirstBinary(Bytes),
Continue(Bytes), Continue(Bytes),
Last(Bytes), Last(Bytes),
} }
#[doc(hidden)]
#[deprecated(since = "4.0.0", note = "Renamed to `ContinuationItem`.")]
pub type Item = ContinuationItem;
/// WebSocket protocol codec. /// WebSocket protocol codec.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Codec { pub struct Codec {
@ -149,7 +153,7 @@ impl Encoder<Message> for Codec {
Parser::write_close(dst, reason, !self.flags.contains(Flags::SERVER)) Parser::write_close(dst, reason, !self.flags.contains(Flags::SERVER))
} }
Message::Continuation(cont) => match cont { Message::Continuation(cont) => match cont {
Item::FirstText(data) => { ContinuationItem::FirstText(data) => {
if self.flags.contains(Flags::W_CONTINUATION) { if self.flags.contains(Flags::W_CONTINUATION) {
return Err(ProtocolError::ContinuationStarted); return Err(ProtocolError::ContinuationStarted);
} else { } else {
@ -163,7 +167,7 @@ impl Encoder<Message> for Codec {
) )
} }
} }
Item::FirstBinary(data) => { ContinuationItem::FirstBinary(data) => {
if self.flags.contains(Flags::W_CONTINUATION) { if self.flags.contains(Flags::W_CONTINUATION) {
return Err(ProtocolError::ContinuationStarted); return Err(ProtocolError::ContinuationStarted);
} else { } else {
@ -177,7 +181,7 @@ impl Encoder<Message> for Codec {
) )
} }
} }
Item::Continue(data) => { ContinuationItem::Continue(data) => {
if self.flags.contains(Flags::W_CONTINUATION) { if self.flags.contains(Flags::W_CONTINUATION) {
Parser::write_message( Parser::write_message(
dst, dst,
@ -190,7 +194,7 @@ impl Encoder<Message> for Codec {
return Err(ProtocolError::ContinuationNotStarted); return Err(ProtocolError::ContinuationNotStarted);
} }
} }
Item::Last(data) => { ContinuationItem::Last(data) => {
if self.flags.contains(Flags::W_CONTINUATION) { if self.flags.contains(Flags::W_CONTINUATION) {
self.flags.remove(Flags::W_CONTINUATION); self.flags.remove(Flags::W_CONTINUATION);
Parser::write_message( Parser::write_message(
@ -223,7 +227,7 @@ impl Decoder for Codec {
return match opcode { return match opcode {
OpCode::Continue => { OpCode::Continue => {
if self.flags.contains(Flags::CONTINUATION) { if self.flags.contains(Flags::CONTINUATION) {
Ok(Some(Frame::Continuation(Item::Continue( Ok(Some(Frame::Continuation(ContinuationItem::Continue(
payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new), payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new),
)))) ))))
} else { } else {
@ -233,7 +237,7 @@ impl Decoder for Codec {
OpCode::Binary => { OpCode::Binary => {
if !self.flags.contains(Flags::CONTINUATION) { if !self.flags.contains(Flags::CONTINUATION) {
self.flags.insert(Flags::CONTINUATION); self.flags.insert(Flags::CONTINUATION);
Ok(Some(Frame::Continuation(Item::FirstBinary( Ok(Some(Frame::Continuation(ContinuationItem::FirstBinary(
payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new), payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new),
)))) ))))
} else { } else {
@ -243,7 +247,7 @@ impl Decoder for Codec {
OpCode::Text => { OpCode::Text => {
if !self.flags.contains(Flags::CONTINUATION) { if !self.flags.contains(Flags::CONTINUATION) {
self.flags.insert(Flags::CONTINUATION); self.flags.insert(Flags::CONTINUATION);
Ok(Some(Frame::Continuation(Item::FirstText( Ok(Some(Frame::Continuation(ContinuationItem::FirstText(
payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new), payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new),
)))) ))))
} else { } else {
@ -261,7 +265,7 @@ impl Decoder for Codec {
OpCode::Continue => { OpCode::Continue => {
if self.flags.contains(Flags::CONTINUATION) { if self.flags.contains(Flags::CONTINUATION) {
self.flags.remove(Flags::CONTINUATION); self.flags.remove(Flags::CONTINUATION);
Ok(Some(Frame::Continuation(Item::Last( Ok(Some(Frame::Continuation(ContinuationItem::Last(
payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new), payload.map(|pl| pl.freeze()).unwrap_or_else(Bytes::new),
)))) ))))
} else { } else {

View File

@ -1,7 +1,7 @@
//! WebSocket protocol implementation. //! WebSocket protocol implementation.
//! //!
//! To setup a WebSocket, first perform the WebSocket handshake then on success convert `Payload` into a //! To setup a WebSocket, first perform the WebSocket handshake then on success convert `Payload`
//! `WsStream` stream and then use `WsWriter` to communicate with the peer. //! into a `WsStream` stream and then use `WsWriter` to communicate with the peer.
use std::io; use std::io;
@ -17,7 +17,8 @@ mod frame;
mod mask; mod mask;
mod proto; mod proto;
pub use self::codec::{Codec, Frame, Item, Message}; #[allow(deprecated)] // Item is deprecated
pub use self::codec::{Codec, ContinuationItem, Frame, Item, Message};
pub use self::dispatcher::Dispatcher; pub use self::dispatcher::Dispatcher;
pub use self::frame::Parser; pub use self::frame::Parser;
pub use self::proto::{hash_key, CloseCode, CloseReason, OpCode}; pub use self::proto::{hash_key, CloseCode, CloseReason, OpCode};

View File

@ -8,7 +8,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Framed};
use actix_http::{ use actix_http::{
body::{BodySize, BoxBody}, body::{BodySize, BoxBody},
h1, h1,
ws::{self, CloseCode, Frame, Item, Message}, ws::{self, CloseCode, ContinuationItem, Frame, Message},
Error, HttpService, Request, Response, Error, HttpService, Request, Response,
}; };
use actix_http_test::test_server; use actix_http_test::test_server;
@ -137,51 +137,61 @@ async fn test_simple() {
assert_eq!(item, Frame::Pong("text".to_string().into())); assert_eq!(item, Frame::Pong("text".to_string().into()));
framed framed
.send(Message::Continuation(Item::FirstText("text".into()))) .send(Message::Continuation(ContinuationItem::FirstText(
"text".into(),
)))
.await .await
.unwrap(); .unwrap();
let item = framed.next().await.unwrap().unwrap(); let item = framed.next().await.unwrap().unwrap();
assert_eq!( assert_eq!(
item, item,
Frame::Continuation(Item::FirstText(Bytes::from_static(b"text"))) Frame::Continuation(ContinuationItem::FirstText(Bytes::from_static(b"text")))
); );
assert!(framed assert!(framed
.send(Message::Continuation(Item::FirstText("text".into()))) .send(Message::Continuation(ContinuationItem::FirstText(
"text".into()
)))
.await .await
.is_err()); .is_err());
assert!(framed assert!(framed
.send(Message::Continuation(Item::FirstBinary("text".into()))) .send(Message::Continuation(ContinuationItem::FirstBinary(
"text".into()
)))
.await .await
.is_err()); .is_err());
framed framed
.send(Message::Continuation(Item::Continue("text".into()))) .send(Message::Continuation(ContinuationItem::Continue(
"text".into(),
)))
.await .await
.unwrap(); .unwrap();
let item = framed.next().await.unwrap().unwrap(); let item = framed.next().await.unwrap().unwrap();
assert_eq!( assert_eq!(
item, item,
Frame::Continuation(Item::Continue(Bytes::from_static(b"text"))) Frame::Continuation(ContinuationItem::Continue(Bytes::from_static(b"text")))
); );
framed framed
.send(Message::Continuation(Item::Last("text".into()))) .send(Message::Continuation(ContinuationItem::Last("text".into())))
.await .await
.unwrap(); .unwrap();
let item = framed.next().await.unwrap().unwrap(); let item = framed.next().await.unwrap().unwrap();
assert_eq!( assert_eq!(
item, item,
Frame::Continuation(Item::Last(Bytes::from_static(b"text"))) Frame::Continuation(ContinuationItem::Last(Bytes::from_static(b"text")))
); );
assert!(framed assert!(framed
.send(Message::Continuation(Item::Continue("text".into()))) .send(Message::Continuation(ContinuationItem::Continue(
"text".into()
)))
.await .await
.is_err()); .is_err());
assert!(framed assert!(framed
.send(Message::Continuation(Item::Last("text".into()))) .send(Message::Continuation(ContinuationItem::Last("text".into())))
.await .await
.is_err()); .is_err());