Skip to content

Commit 53a0f54

Browse files
authored
Merge pull request #460 from lmbollen/ufmt-udisplay-string
Add `uDisplay` for `String` and inline `ufmt` functions
2 parents d2463de + 5785761 commit 53a0f54

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 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

@@ -110,6 +111,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
110111
- `ufmt-impl` is now `ufmt`
111112
- `cas` is removed, atomic polyfilling is now opt-in via the `portable-atomic` feature.
112113
- `Vec::as_mut_slice` is now a public method.
114+
- `ufmt` functions are annotated with `inline`.
113115

114116
### Fixed
115117

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ portable-atomic-unsafe-assume-single-core = ["dep:portable-atomic", "portable-at
3131
serde = ["dep:serde"]
3232

3333
# implement ufmt traits.
34-
ufmt = ["dep:ufmt-write"]
34+
ufmt = ["dep:ufmt", "dep:ufmt-write"]
3535

3636
# Implement `defmt::Format`.
3737
defmt = ["dep:defmt"]
@@ -45,6 +45,7 @@ nightly = []
4545
portable-atomic = { version = "1.0", optional = true }
4646
hash32 = "0.3.0"
4747
serde = { version = "1", optional = true, default-features = false }
48+
ufmt = { version = "0.2", optional = true }
4849
ufmt-write = { version = "0.1", optional = true }
4950
defmt = { version = "1.0.1", optional = true }
5051

@@ -53,7 +54,6 @@ defmt = { version = "1.0.1", optional = true }
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: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@ 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+
#[inline]
11+
fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
12+
where
13+
W: uWrite + ?Sized,
14+
{
15+
f.write_str(&self.as_str())
16+
}
17+
}
18+
819
impl<S: StringStorage + ?Sized> uWrite for StringInner<S> {
920
type Error = CapacityError;
21+
#[inline]
1022
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
1123
self.push_str(s)
1224
}
1325
}
1426

1527
impl<S: VecStorage<u8> + ?Sized> uWrite for VecInner<u8, S> {
1628
type Error = CapacityError;
29+
#[inline]
1730
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
1831
self.extend_from_slice(s.as_bytes())
1932
}
@@ -32,7 +45,16 @@ mod tests {
3245
}
3346

3447
#[test]
35-
fn test_string() {
48+
fn test_udisplay_string() {
49+
let str_a = String::<32>::try_from("world").unwrap();
50+
let mut str_b = String::<32>::new();
51+
uwrite!(str_b, "Hello {}!", str_a).unwrap();
52+
53+
assert_eq!(str_b, "Hello world!");
54+
}
55+
56+
#[test]
57+
fn test_uwrite_string() {
3658
let a = 123;
3759
let b = Pair { x: 0, y: 1234 };
3860

@@ -43,14 +65,14 @@ mod tests {
4365
}
4466

4567
#[test]
46-
fn test_string_err() {
68+
fn test_uwrite_string_err() {
4769
let p = Pair { x: 0, y: 1234 };
4870
let mut s = String::<4>::new();
4971
assert!(uwrite!(s, "{:?}", p).is_err());
5072
}
5173

5274
#[test]
53-
fn test_vec() {
75+
fn test_uwrite_vec() {
5476
let a = 123;
5577
let b = Pair { x: 0, y: 1234 };
5678

0 commit comments

Comments
 (0)