Browse Source

protocol: implement fmt::Write for CursorMut

master
Denis Drakhnia 2 months ago
parent
commit
a5f2affc49
  1. 18
      protocol/src/cursor.rs

18
protocol/src/cursor.rs

@ -1,8 +1,10 @@ @@ -1,8 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: 2023 Denis Drakhnia <numas13@gmail.com>
use std::io::{self, Write as _};
use std::{fmt, mem, str};
use std::{
fmt::{self, Write},
mem, str,
};
use thiserror::Error;
@ -482,9 +484,7 @@ impl<'a> CursorMut<'a> { @@ -482,9 +484,7 @@ impl<'a> CursorMut<'a> {
}
pub fn put_as_str<T: fmt::Display>(&mut self, value: T) -> Result<&mut Self, Error> {
let mut cur = io::Cursor::new(&mut self.buffer[self.pos..]);
write!(&mut cur, "{}", value).map_err(|_| Error::UnexpectedEnd)?;
self.pos += cur.position() as usize;
write!(self, "{}", value).map_err(|_| Error::UnexpectedEnd)?;
Ok(self)
}
@ -507,6 +507,14 @@ impl<'a> CursorMut<'a> { @@ -507,6 +507,14 @@ impl<'a> CursorMut<'a> {
}
}
impl fmt::Write for CursorMut<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.put_bytes(s.as_bytes())
.map(|_| ())
.map_err(|_| fmt::Error)
}
}
#[cfg(test)]
mod tests {
use super::*;

Loading…
Cancel
Save