Skip to content

Commit

Permalink
Add rust package embedded-std with module string
Browse files Browse the repository at this point in the history
Having our own std library with newtypes allows us to implement
traits of `ufmt`.
  • Loading branch information
lmbollen committed Feb 23, 2024
1 parent 50396cb commit c2a1182
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
98 changes: 98 additions & 0 deletions firmware-support/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions firmware-support/embedded-std/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use ufmt::{uDisplay, uWrite};
use heapless::String;

#[derive(Clone)]
pub struct MyString<const SIZE: usize>(pub heapless::String<SIZE>);
pub struct String<const SIZE: usize>(pub heapless::String<SIZE>);

impl <const SIZE: usize> MyString<SIZE> {
impl <const SIZE: usize> String<SIZE> {
pub fn new() -> Self {
MyString(heapless::String::new())
String(heapless::String::new())
}
}

impl<const SIZE: usize> uDisplay for MyString<SIZE> {
impl<const SIZE: usize> uDisplay for String<SIZE> {
#[inline(always)]
fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
where
Expand All @@ -20,7 +19,7 @@ impl<const SIZE: usize> uDisplay for MyString<SIZE> {
}
}

impl <const SIZE: usize> ufmt::uWrite for MyString<SIZE> {
impl <const SIZE: usize> ufmt::uWrite for String<SIZE> {
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
self.0.push_str(s)?;
Ok(())
Expand Down

0 comments on commit c2a1182

Please sign in to comment.