Skip to content

Commit b21563d

Browse files
lmbollenreitermarkus
authored andcommitted
Add the uDisplay trait for String incl test.
Did not add `uDisplay` for `Vec` because there is no trivial implementation. # Conflicts: # Cargo.toml # src/ufmt.rs
1 parent d2463de commit b21563d

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4343
- Implemented `PartialEq` and `Eq` for `Deque`.
4444
- Added `truncate` to `IndexMap`.
4545
- Added `get_index` and `get_index_mut` to `IndexMap`.
46+
- Added `String::uDisplay`.
4647

4748
### Changed
4849

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ hash32 = "0.3.0"
4747
serde = { version = "1", optional = true, default-features = false }
4848
ufmt-write = { version = "0.1", optional = true }
4949
defmt = { version = "1.0.1", optional = true }
50+
ufmt = "0.2"
5051

5152
# for the pool module
5253
[target.'cfg(any(target_arch = "arm", target_pointer_width = "32", target_pointer_width = "64"))'.dependencies]
5354
stable_deref_trait = { version = "1", default-features = false }
5455

5556
[dev-dependencies]
56-
ufmt = "0.2"
5757
static_assertions = "1.1.0"
5858

5959
[package.metadata.docs.rs]

src/ufmt.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ use crate::{
33
vec::{VecInner, VecStorage},
44
CapacityError,
55
};
6+
use ufmt::uDisplay;
67
use ufmt_write::uWrite;
78

9+
impl<S: StringStorage + ?Sized> uDisplay for StringInner<S> {
10+
fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
11+
where
12+
W: uWrite + ?Sized,
13+
{
14+
f.write_str(&self.as_str())
15+
}
16+
}
17+
818
impl<S: StringStorage + ?Sized> uWrite for StringInner<S> {
919
type Error = CapacityError;
1020
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
@@ -32,7 +42,16 @@ mod tests {
3242
}
3343

3444
#[test]
35-
fn test_string() {
45+
fn test_udisplay_string() {
46+
let str_a = String::<32>::try_from("world").unwrap();
47+
let mut str_b = String::<32>::new();
48+
uwrite!(str_b, "Hello {}!", str_a).unwrap();
49+
50+
assert_eq!(str_b, "Hello world!");
51+
}
52+
53+
#[test]
54+
fn test_uwrite_string() {
3655
let a = 123;
3756
let b = Pair { x: 0, y: 1234 };
3857

@@ -43,14 +62,14 @@ mod tests {
4362
}
4463

4564
#[test]
46-
fn test_string_err() {
65+
fn test_uwrite_string_err() {
4766
let p = Pair { x: 0, y: 1234 };
4867
let mut s = String::<4>::new();
4968
assert!(uwrite!(s, "{:?}", p).is_err());
5069
}
5170

5271
#[test]
53-
fn test_vec() {
72+
fn test_uwrite_vec() {
5473
let a = 123;
5574
let b = Pair { x: 0, y: 1234 };
5675

0 commit comments

Comments
 (0)