63 lines
1.5 KiB
Rust
63 lines
1.5 KiB
Rust
use core::{convert::Infallible, fmt::Display, ops::Deref};
|
|
|
|
use arrayvec::ArrayVec;
|
|
use embassy_futures::yield_now;
|
|
use embassy_net::Stack;
|
|
|
|
pub struct WriteBuffer<const CAP: usize>(ArrayVec<u8, CAP>);
|
|
|
|
impl<const CAP: usize> Display for WriteBuffer<CAP> {
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
f.write_str(str::from_utf8(self).map_err(|_| core::fmt::Error)?)
|
|
}
|
|
}
|
|
|
|
impl<const CAP: usize> Deref for WriteBuffer<CAP> {
|
|
type Target = [u8];
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl<const CAP: usize> WriteBuffer<CAP> {
|
|
pub fn new() -> Self {
|
|
Self(ArrayVec::new())
|
|
}
|
|
|
|
pub fn clear(&mut self) {
|
|
self.0.clear();
|
|
}
|
|
}
|
|
|
|
impl<const CAP: usize> embedded_io::ErrorType for WriteBuffer<CAP> {
|
|
type Error = Infallible;
|
|
}
|
|
|
|
impl<const CAP: usize> embedded_io::Write for WriteBuffer<CAP> {
|
|
#[inline]
|
|
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
|
let _ = self.0.try_extend_from_slice(buf); //silently fails!
|
|
Ok(buf.len())
|
|
}
|
|
|
|
#[inline]
|
|
fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
|
|
self.write(buf)?;
|
|
Ok(())
|
|
}
|
|
|
|
#[inline]
|
|
fn flush(&mut self) -> Result<(), Self::Error> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
pub async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 {
|
|
loop {
|
|
if let Some(config) = stack.config_v4() {
|
|
return config.clone();
|
|
}
|
|
yield_now().await;
|
|
}
|
|
}
|