-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ser: Add to_writer function to write to impl of embedded_io::Write
- Loading branch information
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
pub struct MyWriter { | ||
pub buffer: [u8; 128], | ||
pub pos: usize, | ||
pub fail: bool | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct MyWriterError { } | ||
|
||
impl embedded_io::Error for MyWriterError { | ||
fn kind(&self) -> embedded_io::ErrorKind { | ||
embedded_io::ErrorKind::OutOfMemory | ||
} | ||
} | ||
|
||
impl embedded_io::ErrorType for MyWriter { | ||
type Error = MyWriterError; | ||
} | ||
|
||
impl embedded_io::Write for MyWriter { | ||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { | ||
let av = self.buffer.len() - self.pos; | ||
let wr = core::cmp::min(av, buf.len()); | ||
self.buffer[self.pos..(self.pos + wr)].copy_from_slice(&buf[..wr]); | ||
self.pos = self.pos + wr; | ||
Ok(wr) | ||
} | ||
|
||
fn flush(&mut self) -> Result<(), Self::Error> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use super::{Result, Error, ser_backend::SerializerBackend}; | ||
use embedded_io::{Write, self}; | ||
|
||
pub struct WriteSerializer<'a, W: Write> { | ||
writer: &'a mut W, | ||
current_length: usize, | ||
} | ||
|
||
impl<'a, W: Write> WriteSerializer<'a, W> { | ||
/// Create a new `Serializer` | ||
pub fn new(writer: &'a mut W) -> Self { | ||
Self { | ||
writer, | ||
current_length: 0 | ||
} | ||
} | ||
} | ||
|
||
impl<'a, W: Write> SerializerBackend for WriteSerializer<'a, W> { | ||
fn end(&self) -> usize { | ||
self.current_length | ||
} | ||
|
||
fn push(&mut self, c: u8) -> Result<()> { | ||
self.writer.write_all(&[c; 1]).map_err(|_err| Error::IOError)?; | ||
self.current_length = self.current_length + 1; | ||
Ok(()) | ||
} | ||
|
||
fn extend_from_slice(&mut self, other: &[u8]) -> Result<()> { | ||
self.writer.write_all(other).map_err(|_err| Error::IOError)?; | ||
self.current_length = self.current_length + other.len(); | ||
Ok(()) | ||
} | ||
} |