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 28, 2024
1 parent 5d243e3 commit dff18dc
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 1 deletion.
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.

3 changes: 2 additions & 1 deletion firmware-support/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# SPDX-FileCopyrightText: 2022 Google LLC
# SPDX-FileCopyrightText: 2022-2024 Google LLC
#
# SPDX-License-Identifier: CC0-1.0

[workspace]
members = [
"bittide-sys",
"embedded-std",
]
resolver = "2"
30 changes: 30 additions & 0 deletions firmware-support/embedded-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# SPDX-FileCopyrightText: 2024 Google LLC
#
# SPDX-License-Identifier: CC0-1.0

[package]
name = "embedded-std"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
authors = ["Google LLC"]
resolver = "2"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
panic-handler = []
default = []

[dependencies]
ufmt = "0.2.0"
heapless = { version = "0.7", default-features = false}

[dev-dependencies]
proptest = "1.0"
object = { version = "0.28", features = ["write"] }
libc = "0.2"
test-strategy = "0.2.0"
rand = "0.8"
lazy_static = "1.0"
tempfile = "3"
7 changes: 7 additions & 0 deletions firmware-support/embedded-std/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2024 Google LLC
//
// SPDX-License-Identifier: Apache-2.0

#![no_std]

pub mod string;
31 changes: 31 additions & 0 deletions firmware-support/embedded-std/src/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2024 Google LLC
//
// SPDX-License-Identifier: Apache-2.0
use ufmt::{uDisplay, uWrite};

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

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

impl<const SIZE: usize> uDisplay for String<SIZE> {
#[inline(always)]
fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
where
W: uWrite + ?Sized,
{
f.write_str(&self.0.as_str())
}
}

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(())
}
type Error = ();
}

0 comments on commit dff18dc

Please sign in to comment.