async_mp4/
bytes_read.rs

1use std::fmt::Debug;
2
3use async_trait::async_trait;
4use byteorder_async::{BigEndian};
5use futures::{AsyncRead, AsyncSeek};
6use byteorder_async::ReaderToByteOrder;
7use crate::bytes_write::FlagTrait;
8
9use crate::error::MP4Error;
10
11#[async_trait]
12pub trait Mp4Readable: Sized {
13    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error>;
14}
15
16#[async_trait]
17pub trait Mp4VersionedReadable<F: FlagTrait>: Sized {
18    async fn versioned_read<R: ReadMp4>(version: u8, flags: F, reader: &mut R) -> Result<Self, MP4Error>;
19}
20
21#[async_trait]
22impl<F: FlagTrait, T: Mp4Readable> Mp4VersionedReadable<F> for  T {
23    async fn versioned_read<R: ReadMp4>(_: u8, _: F, reader: &mut R) -> Result<Self, MP4Error> {
24        T::read(reader).await
25    }
26}
27
28#[async_trait]
29impl Mp4Readable for u8 {
30    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
31        Ok(reader.byte_order().read_u8().await?)
32    }
33}
34
35#[async_trait]
36impl Mp4Readable for u16 {
37    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
38        Ok(reader.byte_order().read_u16::<BigEndian>().await?)
39    }
40}
41#[async_trait]
42impl Mp4Readable for u32 {
43    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
44        Ok(reader.byte_order().read_u32::<BigEndian>().await?)
45    }
46}
47#[async_trait]
48impl Mp4Readable for u64 {
49    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
50        Ok(reader.byte_order().read_u64::<BigEndian>().await?)
51    }
52}
53
54#[async_trait]
55impl Mp4Readable for i8 {
56    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
57        Ok(reader.byte_order().read_i8().await?)
58    }
59}
60
61#[async_trait]
62impl Mp4Readable for i16 {
63    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
64        Ok(reader.byte_order().read_i16::<BigEndian>().await?)
65    }
66}
67#[async_trait]
68impl Mp4Readable for i32 {
69    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
70        Ok(reader.byte_order().read_i32::<BigEndian>().await?)
71    }
72}
73#[async_trait]
74impl Mp4Readable for i64 {
75    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
76        Ok(reader.byte_order().read_i64::<BigEndian>().await?)
77    }
78}
79
80#[async_trait]
81impl<const N: usize, T: Mp4Readable + Send + Sync + Debug> Mp4Readable for [T; N] {
82    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
83        let mut out = vec![];
84        for _ in 0..N {
85            out.push(T::read(reader).await?);
86        }
87        Ok(out.try_into().unwrap())
88    }
89}
90
91
92#[async_trait]
93pub trait ReadMp4: AsyncRead + AsyncSeek + Unpin + Send + Sync + Sized {
94
95    #[inline]
96    async fn read_u24(&mut self) -> Result<u32, MP4Error> {
97        Ok(self.byte_order().read_u24::<BigEndian>().await?)
98    }
99
100    #[inline]
101    async fn read_i24(&mut self) -> Result<i32, MP4Error> {
102        Ok(self.byte_order().read_i24::<BigEndian>().await?)
103    }
104
105    async fn read<T: Mp4Readable>(&mut self) -> Result<T, MP4Error> {
106        T::read(self).await
107    }
108
109    async fn versioned_read<T: Mp4VersionedReadable<F>, F: FlagTrait>(&mut self, version: u8, flags: F) -> Result<T, MP4Error> { T::versioned_read(version, flags, self).await }
110}
111
112impl<T: AsyncRead + AsyncSeek + Unpin + Send + Sync> ReadMp4 for T {}