use std::fmt;
use tokio::io::{AsyncRead, AsyncWrite};
use crate::{
codec::{DecodeFrom, EncodeInto},
varint::VarInt,
};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct StreamId(pub VarInt);
impl<S: AsyncWrite + Send> EncodeInto<S> for StreamId {
type Output = ();
type Error = <VarInt as EncodeInto<S>>::Error;
async fn encode_into(self, stream: S) -> Result<Self::Output, Self::Error> {
self.0.encode_into(stream).await
}
}
impl<S: AsyncRead + Send> DecodeFrom<S> for StreamId {
type Error = <VarInt as DecodeFrom<S>>::Error;
async fn decode_from(stream: S) -> Result<Self, Self::Error> {
VarInt::decode_from(stream).await.map(Self)
}
}
impl From<VarInt> for StreamId {
fn from(varint: VarInt) -> Self {
StreamId(varint)
}
}
impl From<StreamId> for VarInt {
fn from(stream_id: StreamId) -> Self {
stream_id.0
}
}
impl StreamId {
pub const fn into_inner(self) -> u64 {
self.0.into_inner()
}
}
impl TryFrom<u64> for StreamId {
type Error = crate::varint::err::Overflow;
fn try_from(value: u64) -> Result<Self, Self::Error> {
Ok(Self(VarInt::try_from(value)?))
}
}
impl fmt::Display for StreamId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}