Skip to content
Merged
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
12 changes: 8 additions & 4 deletions src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl VInt64 {
}

impl ReadFrom for VInt64 {
fn read_from<R: std::io::Read>(r: &mut R) -> crate::Result<Self> {
fn read_from<R: std::io::Read + ?Sized>(r: &mut R) -> crate::Result<Self> {
let mut first_byte_buf = [0u8; 1];
r.read_exact(&mut first_byte_buf)?;
let first_byte = first_byte_buf[0];
Expand Down Expand Up @@ -162,7 +162,9 @@ impl ReadFrom for VInt64 {
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl crate::io::tokio_impl::AsyncReadFrom for VInt64 {
async fn async_read_from<R: tokio::io::AsyncRead + Unpin>(r: &mut R) -> crate::Result<Self> {
async fn async_read_from<R: tokio::io::AsyncRead + Unpin + ?Sized>(
r: &mut R,
) -> crate::Result<Self> {
let mut first_byte_buf = [0u8; 1];
tokio::io::AsyncReadExt::read_exact(r, &mut first_byte_buf).await?;
let first_byte = first_byte_buf[0];
Expand Down Expand Up @@ -391,7 +393,7 @@ pub struct Header {
}

impl ReadFrom for Header {
fn read_from<R: std::io::Read>(reader: &mut R) -> crate::Result<Self> {
fn read_from<R: std::io::Read + ?Sized>(reader: &mut R) -> crate::Result<Self> {
let id = VInt64::read_from(reader)?;
let size = VInt64::read_from(reader)?;
Ok(Self { id, size })
Expand All @@ -401,7 +403,9 @@ impl ReadFrom for Header {
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl crate::io::tokio_impl::AsyncReadFrom for Header {
async fn async_read_from<R: tokio::io::AsyncRead + Unpin>(r: &mut R) -> crate::Result<Self> {
async fn async_read_from<R: tokio::io::AsyncRead + Unpin + ?Sized>(
r: &mut R,
) -> crate::Result<Self> {
let id = VInt64::async_read_from(r).await?;
let size = VInt64::async_read_from(r).await?;
Ok(Self { id, size })
Expand Down
6 changes: 4 additions & 2 deletions src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<T: Element> Encode for T {
}

impl<T: Element> ReadFrom for T {
fn read_from<R: std::io::Read>(r: &mut R) -> crate::Result<Self> {
fn read_from<R: std::io::Read + ?Sized>(r: &mut R) -> crate::Result<Self> {
let header = Header::read_from(r)?;
let body = header.read_body(r)?;
let element = match T::decode_body(&mut &body[..]) {
Expand All @@ -74,7 +74,9 @@ impl<T: Element> ReadFrom for T {
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl<T: Element> crate::io::tokio_impl::AsyncReadFrom for T {
async fn async_read_from<R: tokio::io::AsyncRead + Unpin>(r: &mut R) -> crate::Result<Self> {
async fn async_read_from<R: tokio::io::AsyncRead + Unpin + ?Sized>(
r: &mut R,
) -> crate::Result<Self> {
let header = Header::async_read_from(r).await?;
let body = header.read_body_tokio(r).await?;
let element = match T::decode_body(&mut &body[..]) {
Expand Down
28 changes: 16 additions & 12 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ pub mod blocking_impl {
/// Read from a reader.
pub trait ReadFrom: Sized {
/// Read Self from a reader.
fn read_from<R: Read>(r: &mut R) -> crate::Result<Self>;
fn read_from<R: Read + ?Sized>(r: &mut R) -> crate::Result<Self>;
}

/// Read an element from a reader provided the header.
pub trait ReadElement: Sized + Element {
/// Read an element from a reader provided the header.
fn read_element<R: Read>(header: &Header, r: &mut R) -> crate::Result<Self> {
fn read_element<R: Read + ?Sized>(header: &Header, r: &mut R) -> crate::Result<Self> {
let body = header.read_body(r)?;
Self::decode_body(&mut &body[..])
}
Expand All @@ -28,7 +28,7 @@ pub mod blocking_impl {

impl Header {
/// Read the body of the element from a reader into memory.
pub(crate) fn read_body<R: Read>(&self, r: &mut R) -> crate::Result<Vec<u8>> {
pub(crate) fn read_body<R: Read + ?Sized>(&self, r: &mut R) -> crate::Result<Vec<u8>> {
// Segment and Cluster can have unknown size, but we don't support that here.
let size = if self.size.is_unknown && [Segment::ID, Cluster::ID].contains(&self.id) {
return Err(crate::Error::ElementBodySizeUnknown(self.id));
Expand All @@ -49,11 +49,11 @@ pub mod blocking_impl {
/// Write to a writer.
pub trait WriteTo {
/// Write to a writer.
fn write_to<W: Write>(&self, w: &mut W) -> crate::Result<()>;
fn write_to<W: Write + ?Sized>(&self, w: &mut W) -> crate::Result<()>;
}

impl<T: Encode> WriteTo for T {
fn write_to<W: Write>(&self, w: &mut W) -> crate::Result<()> {
fn write_to<W: Write + ?Sized>(&self, w: &mut W) -> crate::Result<()> {
//TODO should avoid the extra allocation here
let mut buf = vec![];
self.encode(&mut buf)?;
Expand All @@ -65,7 +65,11 @@ pub mod blocking_impl {
/// Write an element to a writer provided the header.
pub trait WriteElement: Sized + Element {
/// Write an element to a writer.
fn write_element<W: Write>(&self, header: &Header, w: &mut W) -> crate::Result<()> {
fn write_element<W: Write + ?Sized>(
&self,
header: &Header,
w: &mut W,
) -> crate::Result<()> {
header.write_to(w)?;
let mut buf = vec![];
self.encode_body(&mut buf)?;
Expand All @@ -91,15 +95,15 @@ pub mod tokio_impl {
/// Read from a reader asynchronously.
pub trait AsyncReadFrom: Sized {
/// Read Self from a reader.
fn async_read_from<R: tokio::io::AsyncRead + Unpin>(
fn async_read_from<R: tokio::io::AsyncRead + Unpin + ?Sized>(
r: &mut R,
) -> impl Future<Output = crate::Result<Self>>;
}

/// Read an element from a reader provided the header asynchronously.
pub trait AsyncReadElement: Sized + Element {
/// Read an element from a reader provided the header.
fn async_read_element<R: tokio::io::AsyncRead + Unpin>(
fn async_read_element<R: tokio::io::AsyncRead + Unpin + ?Sized>(
header: &Header,
r: &mut R,
) -> impl std::future::Future<Output = crate::Result<Self>> {
Expand All @@ -114,14 +118,14 @@ pub mod tokio_impl {
/// Write to a writer asynchronously.
pub trait AsyncWriteTo {
/// Write to a writer asynchronously.
fn async_write_to<W: tokio::io::AsyncWrite + Unpin>(
fn async_write_to<W: tokio::io::AsyncWrite + Unpin + ?Sized>(
&self,
w: &mut W,
) -> impl std::future::Future<Output = crate::Result<()>>;
}

impl<T: crate::functional::Encode> AsyncWriteTo for T {
async fn async_write_to<W: tokio::io::AsyncWrite + Unpin>(
async fn async_write_to<W: tokio::io::AsyncWrite + Unpin + ?Sized>(
&self,
w: &mut W,
) -> crate::Result<()> {
Expand All @@ -135,7 +139,7 @@ pub mod tokio_impl {
/// Write an element to a writer provided the header asynchronously.
pub trait AsyncWriteElement: Sized + Element {
/// Write an element to a writer asynchronously.
fn async_write_element<W: tokio::io::AsyncWrite + Unpin>(
fn async_write_element<W: tokio::io::AsyncWrite + Unpin + ?Sized>(
&self,
header: &Header,
w: &mut W,
Expand All @@ -152,7 +156,7 @@ pub mod tokio_impl {

impl Header {
/// Read the body of the element from a reader into memory.
pub(crate) async fn read_body_tokio<R: AsyncRead + Unpin>(
pub(crate) async fn read_body_tokio<R: AsyncRead + Unpin + ?Sized>(
&self,
r: &mut R,
) -> crate::Result<Vec<u8>> {
Expand Down
8 changes: 4 additions & 4 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl MatroskaView {
/// but skipping Cluster data to avoid loading it into memory.
pub fn new<R>(reader: &mut R) -> crate::Result<Self>
where
R: std::io::Read + std::io::Seek,
R: std::io::Read + std::io::Seek + ?Sized,
{
use crate::io::blocking_impl::*;

Expand All @@ -43,7 +43,7 @@ impl MatroskaView {
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub async fn new_async<R>(reader: &mut R) -> crate::Result<Self>
where
R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin,
R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin + ?Sized,
{
use crate::io::tokio_impl::*;

Expand Down Expand Up @@ -85,7 +85,7 @@ impl SegmentView {
/// but skipping Cluster data to avoid loading it into memory.
pub fn new<R>(reader: &mut R) -> crate::Result<Vec<Self>>
where
R: std::io::Read + std::io::Seek,
R: std::io::Read + std::io::Seek + ?Sized,
{
let mut out = vec![];

Expand Down Expand Up @@ -214,7 +214,7 @@ impl SegmentView {
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
pub async fn new_async<R>(reader: &mut R) -> crate::Result<Vec<Self>>
where
R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin,
R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin + ?Sized,
{
let mut out = vec![];

Expand Down
Loading