Skip to content

Commit b470e26

Browse files
bors[bot]dbrgn
andauthored
Merge #156
156: Add optional ufmt impls r=japaric a=dbrgn By enabling the `ufmt-impl` feature, `uWrite` impls are provided for `String<N>` and `Vec<u8, N>`. Co-authored-by: Danilo Bargen <mail@dbrgn.ch>
2 parents 75bcd7e + 92d1a39 commit b470e26

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ version = "0.5.4"
2222
[features]
2323
default = ["cas"]
2424
cas = []
25+
ufmt-impl = ["ufmt-write"]
2526
# only for tests
2627
__trybuild = []
2728

@@ -41,3 +42,10 @@ default-features = false
4142
[dependencies.stable_deref_trait]
4243
version = "1"
4344
default-features = false
45+
46+
[dependencies.ufmt-write]
47+
version = "0.1"
48+
optional = true
49+
50+
[dev-dependencies.ufmt]
51+
version = "0.1"

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
//! - [`mpmc::Q*`](mpmc/index.html) -- multiple producer multiple consumer lock-free queue
5757
//! - [`spsc::Queue`](spsc/struct.Queue.html) -- single producer single consumer lock-free queue
5858
//!
59+
//! # Optional Features
60+
//!
61+
//! The `heapless` crate provides the following optional Cargo features:
62+
//!
63+
//! - `ufmt-impl`: Implement [`ufmt_write::uWrite`] for `String<N>` and `Vec<u8, N>`
64+
//!
65+
//! [`ufmt_write::uWrite`]: https://docs.rs/ufmt-write/
66+
//!
5967
//! # Minimum Supported Rust Version (MSRV)
6068
//!
6169
//! This crate is guaranteed to compile on stable Rust 1.36 and up with its default set of features.
@@ -99,4 +107,7 @@ pub mod pool;
99107
#[cfg(has_atomics)]
100108
pub mod spsc;
101109

110+
#[cfg(feature = "ufmt-impl")]
111+
mod ufmt;
112+
102113
mod sealed;

src/ufmt.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use ufmt_write::uWrite;
2+
3+
use crate::{
4+
ArrayLength,
5+
string::String,
6+
vec::Vec,
7+
};
8+
9+
impl<N> uWrite for String<N>
10+
where
11+
N: ArrayLength<u8>,
12+
{
13+
type Error = ();
14+
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
15+
self.push_str(s)
16+
}
17+
}
18+
19+
impl<N> uWrite for Vec<u8, N>
20+
where
21+
N: ArrayLength<u8>,
22+
{
23+
type Error = ();
24+
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
25+
self.extend_from_slice(s.as_bytes())
26+
}
27+
}
28+
29+
#[cfg(test)]
30+
mod tests {
31+
use super::*;
32+
33+
use ufmt::{derive::uDebug, uwrite};
34+
35+
use crate::consts::*;
36+
37+
#[derive(uDebug)]
38+
struct Pair {
39+
x: u32,
40+
y: u32,
41+
}
42+
43+
#[test]
44+
fn test_string() {
45+
let a = 123;
46+
let b = Pair { x: 0, y: 1234 };
47+
48+
let mut s = String::<U32>::new();
49+
uwrite!(s, "{} -> {:?}", a, b).unwrap();
50+
51+
assert_eq!(s, "123 -> Pair { x: 0, y: 1234 }");
52+
}
53+
54+
#[test]
55+
fn test_string_err() {
56+
let p = Pair { x: 0, y: 1234 };
57+
let mut s = String::<U4>::new();
58+
assert!(uwrite!(s, "{:?}", p).is_err());
59+
}
60+
61+
#[test]
62+
fn test_vec() {
63+
let a = 123;
64+
let b = Pair { x: 0, y: 1234 };
65+
66+
let mut v = Vec::<u8, U32>::new();
67+
uwrite!(v, "{} -> {:?}", a, b).unwrap();
68+
69+
assert_eq!(v, b"123 -> Pair { x: 0, y: 1234 }");
70+
}
71+
}

0 commit comments

Comments
 (0)