1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-18 04:33:12 +01:00

prepare actix-codec release

This commit is contained in:
Nikolay Kim 2019-12-11 10:18:11 +06:00
parent bf734a31dc
commit 32202188cc
3 changed files with 20 additions and 36 deletions

View File

@ -1,5 +1,9 @@
# Changes # Changes
## [0.2.0] - 2019-12-10
* Use specific futures dependencies
## [0.2.0-alpha.4] ## [0.2.0-alpha.4]
* Fix buffer remaining capacity calcualtion * Fix buffer remaining capacity calcualtion
@ -14,17 +18,14 @@
* Migrated to `std::future` * Migrated to `std::future`
## [0.1.2] - 2019-03-27 ## [0.1.2] - 2019-03-27
* Added `Framed::map_io()` method. * Added `Framed::map_io()` method.
## [0.1.1] - 2019-03-06 ## [0.1.1] - 2019-03-06
* Added `FramedParts::with_read_buffer()` method. * Added `FramedParts::with_read_buffer()` method.
## [0.1.0] - 2018-12-09 ## [0.1.0] - 2018-12-09
* Move codec to separate crate * Move codec to separate crate

View File

@ -1,6 +1,6 @@
[package] [package]
name = "actix-codec" name = "actix-codec"
version = "0.2.0-alpha.4" version = "0.2.0"
authors = ["Nikolay Kim <fafhrd91@gmail.com>"] authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
description = "Utilities for encoding and decoding frames" description = "Utilities for encoding and decoding frames"
keywords = ["network", "framework", "async", "futures"] keywords = ["network", "framework", "async", "futures"]
@ -9,7 +9,6 @@ repository = "https://github.com/actix/actix-net.git"
documentation = "https://docs.rs/actix-codec/" documentation = "https://docs.rs/actix-codec/"
categories = ["network-programming", "asynchronous"] categories = ["network-programming", "asynchronous"]
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
edition = "2018" edition = "2018"
workspace = ".." workspace = ".."
@ -20,7 +19,8 @@ path = "src/lib.rs"
[dependencies] [dependencies]
bitflags = "1.2.1" bitflags = "1.2.1"
bytes = "0.5.2" bytes = "0.5.2"
futures = "0.3.1" futures-core = "0.3.1"
futures-sink = "0.3.1"
tokio = { version = "0.2.4", default-features=false } tokio = { version = "0.2.4", default-features=false }
tokio-util = { version = "0.2.0", default-features=false, features=["codec"] } tokio-util = { version = "0.2.0", default-features=false, features=["codec"] }
log = "0.4" log = "0.4"

View File

@ -3,7 +3,8 @@ use std::task::{Context, Poll};
use std::{fmt, io}; use std::{fmt, io};
use bytes::BytesMut; use bytes::BytesMut;
use futures::{ready, Sink, Stream}; use futures_core::{ready, Stream};
use futures_sink::Sink;
use crate::{AsyncRead, AsyncWrite, Decoder, Encoder}; use crate::{AsyncRead, AsyncWrite, Decoder, Encoder};
@ -19,8 +20,6 @@ bitflags::bitflags! {
/// A unified `Stream` and `Sink` interface to an underlying I/O object, using /// A unified `Stream` and `Sink` interface to an underlying I/O object, using
/// the `Encoder` and `Decoder` traits to encode and decode frames. /// the `Encoder` and `Decoder` traits to encode and decode frames.
///
/// You can create a `Framed` instance by using the `AsyncRead::framed` adapter.
pub struct Framed<T, U> { pub struct Framed<T, U> {
io: T, io: T,
codec: U, codec: U,
@ -49,10 +48,6 @@ where
/// `Sink`; grouping this into a single object is often useful for layering /// `Sink`; grouping this into a single object is often useful for layering
/// things like gzip or TLS, which require both read and write access to the /// things like gzip or TLS, which require both read and write access to the
/// underlying object. /// underlying object.
///
/// If you want to work more directly with the streams and sink, consider
/// calling `split` on the `Framed` returned by this method, which will
/// break them into separate objects, allowing them to interact more easily.
pub fn new(io: T, codec: U) -> Framed<T, U> { pub fn new(io: T, codec: U) -> Framed<T, U> {
Framed { Framed {
io, io,
@ -82,10 +77,6 @@ impl<T, U> Framed<T, U> {
/// This objects takes a stream and a readbuffer and a writebuffer. These /// This objects takes a stream and a readbuffer and a writebuffer. These
/// field can be obtained from an existing `Framed` with the /// field can be obtained from an existing `Framed` with the
/// `into_parts` method. /// `into_parts` method.
///
/// If you want to work more directly with the streams and sink, consider
/// calling `split` on the `Framed` returned by this method, which will
/// break them into separate objects, allowing them to interact more easily.
pub fn from_parts(parts: FramedParts<T, U>) -> Framed<T, U> { pub fn from_parts(parts: FramedParts<T, U>) -> Framed<T, U> {
Framed { Framed {
io: parts.io, io: parts.io,
@ -136,15 +127,6 @@ impl<T, U> Framed<T, U> {
self.write_buf.len() >= HW self.write_buf.len() >= HW
} }
/// Consumes the `Frame`, returning its underlying I/O stream.
///
/// Note that care should be taken to not tamper with the underlying stream
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn into_inner(self) -> T {
self.io
}
/// Consume the `Frame`, returning `Frame` with different codec. /// Consume the `Frame`, returning `Frame` with different codec.
pub fn into_framed<U2>(self, codec: U2) -> Framed<T, U2> { pub fn into_framed<U2>(self, codec: U2) -> Framed<T, U2> {
Framed { Framed {
@ -217,11 +199,14 @@ impl<T, U> Framed<T, U> {
Ok(()) Ok(())
} }
/// Check if framed is able to write more data /// Check if framed is able to write more data.
pub fn is_ready(&self) -> bool { ///
/// `Framed` object considers ready if there is free space in write buffer.
pub fn is_write_ready(&self) -> bool {
self.write_buf.len() < HW self.write_buf.len() < HW
} }
/// Try to read underlying I/O stream and decode item.
pub fn next_item(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<U::Item, U::Error>>> pub fn next_item(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<U::Item, U::Error>>>
where where
T: AsyncRead, T: AsyncRead,
@ -251,9 +236,7 @@ impl<T, U> Framed<T, U> {
return Poll::Ready(Some(Ok(frame))); return Poll::Ready(Some(Ok(frame)));
} }
Err(e) => return Poll::Ready(Some(Err(e))), Err(e) => return Poll::Ready(Some(Err(e))),
_ => { _ => (), // Need more data
// Need more data
}
} }
self.flags.remove(Flags::READABLE); self.flags.remove(Flags::READABLE);
@ -281,6 +264,7 @@ impl<T, U> Framed<T, U> {
} }
} }
/// Flush write buffer to underlying I/O stream.
pub fn flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>> pub fn flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
where where
T: AsyncWrite, T: AsyncWrite,
@ -298,14 +282,12 @@ impl<T, U> Framed<T, U> {
if n == 0 { if n == 0 {
return Poll::Ready(Err(io::Error::new( return Poll::Ready(Err(io::Error::new(
io::ErrorKind::WriteZero, io::ErrorKind::WriteZero,
"failed to \ "failed to write frame to transport",
write frame to transport",
) )
.into())); .into()));
} }
// TODO: Add a way to `bytes` to do this w/o returning the drained // remove written data
// data.
let _ = self.write_buf.split_to(n); let _ = self.write_buf.split_to(n);
} }
@ -316,6 +298,7 @@ impl<T, U> Framed<T, U> {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} }
/// Flush write buffer and shutdown underlying I/O stream.
pub fn close(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>> pub fn close(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
where where
T: AsyncWrite, T: AsyncWrite,
@ -350,7 +333,7 @@ where
type Error = U::Error; type Error = U::Error;
fn poll_ready(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
if self.is_ready() { if self.is_write_ready() {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} else { } else {
Poll::Pending Poll::Pending