1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
//! Operations on the WebTransport streams.
use core::future::Future;
use crate::utils::{maybe, Error};
/// Read the data from the stream.
pub trait Read: maybe::Send {
/// An error that can occur while reading the stream.
type Error: Error + maybe::Send + maybe::Sync + 'static;
/// Read the data from the stream into a given buffer and return the amount
/// of bytes filled in the buffer or `None` if the stream is closed and does
/// not have any pending unread data.
fn read(
&mut self,
buf: &mut [u8],
) -> impl Future<Output = Result<Option<usize>, Self::Error>> + maybe::Send;
}
/// Write the data to a stream.
pub trait Write: maybe::Send {
/// An error that can occur while writing to the stream.
type Error: Error + maybe::Send + maybe::Sync + 'static;
/// Write the data from the given buffer into the stream, returning
/// the amount of of bytes that were successfully written into the stream.
/// If the returned amount is smaller than the size of the data that was
/// being written, the user should try writing the remaining data again.
fn write(
&mut self,
buf: &[u8],
) -> impl Future<Output = Result<usize, Self::Error>> + maybe::Send;
}
/// An chunk of data with an explicit offset in the stream.
#[derive(Debug)]
pub struct Chunk<Data> {
/// The offset of the data in the stream.
pub offset: u64,
/// The data.
pub data: Data,
}
/// Read a [`Chunk`] from a stream.
pub trait ReadChunk<ChunkType: ?Sized + ReadableChunk>: maybe::Send {
/// An error that can occur while reading a chunk from the stream.
type Error: Error + maybe::Send + maybe::Sync + 'static;
/// Read the data with the offset information from a stream.
fn read_chunk(
&mut self,
max_length: usize,
ordered: bool,
) -> impl Future<
Output = Result<Option<Chunk<<ChunkType as ReadableChunk>::Data<'_>>>, Self::Error>,
> + maybe::Send;
}
/// Write a [`Chunk`] to a stream.
pub trait WriteChunk<ChunkType: ?Sized + WriteableChunk>: maybe::Send {
/// An error that can occur while writing the chunk to a stream.
type Error: Error + maybe::Send + maybe::Sync + 'static;
/// Write the data with the offset information from a stream.
fn write_chunk<'a>(
&'a mut self,
buf: <ChunkType as WriteableChunk>::Data<'a>,
) -> impl Future<Output = Result<(), Self::Error>> + 'a + maybe::Send;
}
/// Something that specifies the data type for a [`Chunk`] that can
/// be read from a stream.
pub trait ReadableChunk: maybe::Send {
/// The type that will hold the read data.
type Data<'a>: AsRef<[u8]>;
}
/// Something that specifies the data type for a [`Chunk`] that can
/// be written to a stream.
pub trait WriteableChunk: maybe::Send {
/// The type that will hold the data to write.
type Data<'a>: From<&'a [u8]>;
}
#[cfg(feature = "alloc")]
pub mod chunk {
//! Provided [`ReadableChunk`] and [`WriteableChunk`] implementations.
use super::*;
/// A chunk type that represents operations that carry the data as [`u8`]
/// [`slice`]s or [`alloc::vec::Vec`]s
#[derive(Debug)]
pub struct U8;
impl WriteableChunk for U8 {
type Data<'b> = &'b [u8];
}
impl ReadableChunk for U8 {
type Data<'b> = alloc::vec::Vec<u8>;
}
}