Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,13 @@ any! {
}

impl ReadFrom for Any {
fn read_from<R: Read>(r: &mut R) -> Result<Self> {
fn read_from<R: Read + ?Sized>(r: &mut R) -> Result<Self> {
<Option<Any> as ReadFrom>::read_from(r)?.ok_or(Error::UnexpectedEof)
}
}

impl ReadFrom for Option<Any> {
fn read_from<R: Read>(r: &mut R) -> Result<Self> {
fn read_from<R: Read + ?Sized>(r: &mut R) -> Result<Self> {
let header = match <Option<Header> as ReadFrom>::read_from(r)? {
Some(header) => header,
None => return Ok(None),
Expand All @@ -376,7 +376,7 @@ impl ReadFrom for Option<Any> {
}

impl ReadAtom for Any {
fn read_atom<R: Read>(header: &Header, r: &mut R) -> Result<Self> {
fn read_atom<R: Read + ?Sized>(header: &Header, r: &mut R) -> Result<Self> {
let body = &mut header.read_body(r)?;
Any::decode_atom(header, body)
}
Expand Down
10 changes: 5 additions & 5 deletions src/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ impl<T: Atom> DecodeMaybe for T {
}

impl<T: Atom> ReadFrom for T {
fn read_from<R: Read>(r: &mut R) -> Result<Self> {
fn read_from<R: Read + ?Sized>(r: &mut R) -> Result<Self> {
<Option<T> as ReadFrom>::read_from(r)?.ok_or(Error::MissingBox(T::KIND))
}
}

impl<T: Atom> ReadFrom for Option<T> {
fn read_from<R: Read>(r: &mut R) -> Result<Self> {
fn read_from<R: Read + ?Sized>(r: &mut R) -> Result<Self> {
let header = match <Option<Header> as ReadFrom>::read_from(r)? {
Some(header) => header,
None => return Ok(None),
Expand All @@ -104,13 +104,13 @@ impl<T: Atom> ReadFrom for Option<T> {
}

impl<T: Atom> ReadUntil for T {
fn read_until<R: Read>(r: &mut R) -> Result<Self> {
fn read_until<R: Read + ?Sized>(r: &mut R) -> Result<Self> {
<Option<T> as ReadUntil>::read_until(r)?.ok_or(Error::MissingBox(T::KIND))
}
}

impl<T: Atom> ReadUntil for Option<T> {
fn read_until<R: Read>(r: &mut R) -> Result<Self> {
fn read_until<R: Read + ?Sized>(r: &mut R) -> Result<Self> {
while let Some(header) = <Option<Header> as ReadFrom>::read_from(r)? {
if header.kind == T::KIND {
let body = &mut header.read_body(r)?;
Expand Down Expand Up @@ -153,7 +153,7 @@ impl<T: Atom> DecodeAtom for T {
}

impl<T: Atom> ReadAtom for T {
fn read_atom<R: Read>(header: &Header, r: &mut R) -> Result<Self> {
fn read_atom<R: Read + ?Sized>(header: &Header, r: &mut R) -> Result<Self> {
if header.kind != T::KIND {
return Err(Error::UnexpectedBox(header.kind));
}
Expand Down
8 changes: 4 additions & 4 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ impl DecodeMaybe for Header {
}

impl ReadFrom for Header {
fn read_from<R: Read>(r: &mut R) -> Result<Self> {
fn read_from<R: Read + ?Sized>(r: &mut R) -> Result<Self> {
<Option<Header> as ReadFrom>::read_from(r)?.ok_or(Error::UnexpectedEof)
}
}

impl ReadFrom for Option<Header> {
fn read_from<R: Read>(r: &mut R) -> Result<Self> {
fn read_from<R: Read + ?Sized>(r: &mut R) -> Result<Self> {
let mut buf = [0u8; 8];
let n = r.read(&mut buf)?;
if n == 0 {
Expand Down Expand Up @@ -107,7 +107,7 @@ impl ReadFrom for Option<Header> {

// Utility methods
impl Header {
pub(crate) fn read_body<R: Read>(&self, r: &mut R) -> Result<Cursor<Vec<u8>>> {
pub(crate) fn read_body<R: Read + ?Sized>(&self, r: &mut R) -> Result<Cursor<Vec<u8>>> {
// TODO This allocates on the heap.
// Ideally, we should use ReadFrom instead of Decode to avoid this.

Expand All @@ -132,7 +132,7 @@ impl Header {
}

#[cfg(feature = "tokio")]
pub(crate) async fn read_body_tokio<R: ::tokio::io::AsyncRead + Unpin>(
pub(crate) async fn read_body_tokio<R: ::tokio::io::AsyncRead + Unpin + ?Sized>(
&self,
r: &mut R,
) -> Result<Cursor<Vec<u8>>> {
Expand Down
10 changes: 5 additions & 5 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ use super::*;

/// Read a type from a reader.
pub trait ReadFrom: Sized {
fn read_from<R: Read>(r: &mut R) -> Result<Self>;
fn read_from<R: Read + ?Sized>(r: &mut R) -> Result<Self>;
}

/// Read an atom from a reader provided the header.
pub trait ReadAtom: Sized {
fn read_atom<R: Read>(header: &Header, r: &mut R) -> Result<Self>;
fn read_atom<R: Read + ?Sized>(header: &Header, r: &mut R) -> Result<Self>;
}

/// Keep discarding atoms until the desired atom is found.
pub trait ReadUntil: Sized {
fn read_until<R: Read>(r: &mut R) -> Result<Self>;
fn read_until<R: Read + ?Sized>(r: &mut R) -> Result<Self>;
}

/// Write a type to a writer.
pub trait WriteTo {
fn write_to<W: Write>(&self, w: &mut W) -> Result<()>;
fn write_to<W: Write + ?Sized>(&self, w: &mut W) -> Result<()>;
}

impl<T: Encode> WriteTo for T {
fn write_to<W: Write>(&self, w: &mut W) -> Result<()> {
fn write_to<W: Write + ?Sized>(&self, w: &mut W) -> Result<()> {
// TODO We should avoid allocating a buffer here.
let mut buf = Vec::new();
self.encode(&mut buf)?;
Expand Down
6 changes: 3 additions & 3 deletions src/tokio/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use crate::{Any, DecodeAtom, Error, Header, Result};
use tokio::io::AsyncRead;

impl AsyncReadFrom for Any {
async fn read_from<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
async fn read_from<R: AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self> {
<Option<Any> as AsyncReadFrom>::read_from(r)
.await?
.ok_or(Error::UnexpectedEof)
}
}

impl AsyncReadFrom for Option<Any> {
async fn read_from<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
async fn read_from<R: AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self> {
let header = match Option::<Header>::read_from(r).await? {
Some(header) => header,
None => return Ok(None),
Expand All @@ -24,7 +24,7 @@ impl AsyncReadFrom for Option<Any> {
}

impl AsyncReadAtom for Any {
async fn read_atom<R: AsyncRead + Unpin>(header: &Header, r: &mut R) -> Result<Self> {
async fn read_atom<R: AsyncRead + Unpin + ?Sized>(header: &Header, r: &mut R) -> Result<Self> {
let mut buf = header.read_body_tokio(r).await?;
Any::decode_atom(header, &mut buf)
}
Expand Down
12 changes: 6 additions & 6 deletions src/tokio/atom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{Atom, Buf, DecodeAtom, Encode, Error, Header, Result};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};

impl<T: Encode> AsyncWriteTo for T {
async fn write_to<W: AsyncWrite + Unpin>(&self, w: &mut W) -> Result<()> {
async fn write_to<W: AsyncWrite + Unpin + ?Sized>(&self, w: &mut W) -> Result<()> {
// TODO We should avoid allocating a buffer here.
let mut buf = Vec::new();
self.encode(&mut buf)?;
Expand All @@ -14,15 +14,15 @@ impl<T: Encode> AsyncWriteTo for T {
}

impl<T: Atom> AsyncReadFrom for T {
async fn read_from<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
async fn read_from<R: AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self> {
<Option<T> as AsyncReadFrom>::read_from(r)
.await?
.ok_or(Error::MissingBox(T::KIND))
}
}

impl<T: Atom> AsyncReadFrom for Option<T> {
async fn read_from<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
async fn read_from<R: AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self> {
let header = match Option::<Header>::read_from(r).await? {
Some(header) => header,
None => return Ok(None),
Expand All @@ -46,15 +46,15 @@ impl<T: Atom> AsyncReadFrom for Option<T> {
}

impl<T: Atom> AsyncReadUntil for T {
async fn read_until<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
async fn read_until<R: AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self> {
Option::<T>::read_until(r)
.await?
.ok_or(Error::MissingBox(T::KIND))
}
}

impl<T: Atom> AsyncReadUntil for Option<T> {
async fn read_until<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
async fn read_until<R: AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self> {
while let Some(header) = Option::<Header>::read_from(r).await? {
if header.kind == T::KIND {
let mut buf = header.read_body_tokio(r).await?;
Expand All @@ -67,7 +67,7 @@ impl<T: Atom> AsyncReadUntil for Option<T> {
}

impl<T: Atom> AsyncReadAtom for T {
async fn read_atom<R: AsyncRead + Unpin>(header: &Header, r: &mut R) -> Result<Self> {
async fn read_atom<R: AsyncRead + Unpin + ?Sized>(header: &Header, r: &mut R) -> Result<Self> {
if header.kind != T::KIND {
return Err(Error::UnexpectedBox(header.kind));
}
Expand Down
4 changes: 2 additions & 2 deletions src/tokio/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use super::*;
use tokio::io::{AsyncRead, AsyncReadExt};

impl AsyncReadFrom for Header {
async fn read_from<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
async fn read_from<R: AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self> {
<Option<Header> as AsyncReadFrom>::read_from(r)
.await?
.ok_or(Error::UnexpectedEof)
}
}

impl AsyncReadFrom for Option<Header> {
async fn read_from<R: AsyncRead + Unpin>(r: &mut R) -> Result<Self> {
async fn read_from<R: AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self> {
let mut buf = [0u8; 8];
let n = r.read(&mut buf).await?;
if n == 0 {
Expand Down
12 changes: 7 additions & 5 deletions src/tokio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ use crate::{Header, Result};

pub trait AsyncReadFrom: Sized {
#[allow(async_fn_in_trait)]
async fn read_from<R: tokio::io::AsyncRead + Unpin>(r: &mut R) -> Result<Self>;
async fn read_from<R: tokio::io::AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self>;
}

pub trait AsyncWriteTo {
#[allow(async_fn_in_trait)]
async fn write_to<W: tokio::io::AsyncWrite + Unpin>(&self, w: &mut W) -> Result<()>;
async fn write_to<W: tokio::io::AsyncWrite + Unpin + ?Sized>(&self, w: &mut W) -> Result<()>;
}

pub trait AsyncReadAtom: Sized {
#[allow(async_fn_in_trait)]
async fn read_atom<R: tokio::io::AsyncRead + Unpin>(header: &Header, r: &mut R)
-> Result<Self>;
async fn read_atom<R: tokio::io::AsyncRead + Unpin + ?Sized>(
header: &Header,
r: &mut R,
) -> Result<Self>;
}

pub trait AsyncReadUntil: Sized {
#[allow(async_fn_in_trait)]
async fn read_until<R: tokio::io::AsyncRead + Unpin>(r: &mut R) -> Result<Self>;
async fn read_until<R: tokio::io::AsyncRead + Unpin + ?Sized>(r: &mut R) -> Result<Self>;
}