use core::future::Future;
use std::io::Result;
use qubit_codec::ByteOrder;
use qubit_io::AsyncOutput;
use crate::AsyncBinaryWriteExt;
use crate::AsyncLeb128WriteExt;
use crate::util::checked_u16_len;
use crate::util::checked_u32_len;
use crate::util::checked_u64_len;
use crate::util::write_all_async;
pub trait AsyncStringWriteExt: AsyncOutput<Item = u8> {
#[inline]
fn write_utf8_payload_async<'a>(&'a mut self, value: &'a str) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move { write_all_async(self, value.as_bytes()).await }
}
#[inline]
fn write_utf8_string_uleb_usize_async<'a>(&'a mut self, value: &'a str) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move {
self.write_uleb_usize_async(value.len()).await?;
write_all_async(self, value.as_bytes()).await
}
}
#[inline]
fn write_utf8_string_uleb_u64_async<'a>(&'a mut self, value: &'a str) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move {
let len = checked_u64_len(value.len())?;
self.write_uleb_u64_async(len).await?;
write_all_async(self, value.as_bytes()).await
}
}
#[inline]
fn write_string_with_u16_len_async<'a>(
&'a mut self,
value: &'a str,
byte_order: ByteOrder,
) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move {
let len = checked_u16_len(value.len())?;
self.write_u16_async(len, byte_order).await?;
write_all_async(self, value.as_bytes()).await
}
}
#[inline]
fn write_string_with_u16_len_be_async<'a>(&'a mut self, value: &'a str) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move {
let len = checked_u16_len(value.len())?;
self.write_u16_be_async(len).await?;
write_all_async(self, value.as_bytes()).await
}
}
#[inline]
fn write_string_with_u16_len_le_async<'a>(&'a mut self, value: &'a str) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move {
let len = checked_u16_len(value.len())?;
self.write_u16_le_async(len).await?;
write_all_async(self, value.as_bytes()).await
}
}
#[inline]
fn write_string_with_u32_len_async<'a>(
&'a mut self,
value: &'a str,
byte_order: ByteOrder,
) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move {
let len = checked_u32_len(value.len())?;
self.write_u32_async(len, byte_order).await?;
write_all_async(self, value.as_bytes()).await
}
}
#[inline]
fn write_string_with_u32_len_be_async<'a>(&'a mut self, value: &'a str) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move {
let len = checked_u32_len(value.len())?;
self.write_u32_be_async(len).await?;
write_all_async(self, value.as_bytes()).await
}
}
#[inline]
fn write_string_with_u32_len_le_async<'a>(&'a mut self, value: &'a str) -> impl Future<Output = Result<()>> + 'a
where
Self: Unpin,
{
async move {
let len = checked_u32_len(value.len())?;
self.write_u32_le_async(len).await?;
write_all_async(self, value.as_bytes()).await
}
}
}
impl<W> AsyncStringWriteExt for W where W: AsyncOutput<Item = u8> + ?Sized {}