use core::{convert::Infallible, fmt::Display, ops::Deref}; use arrayvec::ArrayVec; use embassy_futures::yield_now; use embassy_net::Stack; pub struct WriteBuffer(ArrayVec); impl Display for WriteBuffer { fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { f.write_str(str::from_utf8(self).map_err(|_| core::fmt::Error)?) } } impl Deref for WriteBuffer { type Target = [u8]; fn deref(&self) -> &Self::Target { &self.0 } } impl WriteBuffer { pub fn new() -> Self { Self(ArrayVec::new()) } pub fn clear(&mut self) { self.0.clear(); } } impl embedded_io::ErrorType for WriteBuffer { type Error = Infallible; } impl embedded_io::Write for WriteBuffer { #[inline] fn write(&mut self, buf: &[u8]) -> Result { 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; } }