Skip to content

Commit

Permalink
Add the uDisplay trait for String incl test.
Browse files Browse the repository at this point in the history
Did not add `uDisplay` for `Vec` because there is no trivial implementation.
  • Loading branch information
lmbollen committed Feb 28, 2024
1 parent 483862b commit cf989c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `Extend` impls for `Deque`.
- Added `Deque::make_contiguous`.
- Added `VecView`, the `!Sized` version of `Vec`.
- Added `String::uDisplay`.

### Changed

Expand Down
24 changes: 21 additions & 3 deletions src/ufmt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
use crate::{string::String, vec::Vec};
use ufmt_write::uWrite;

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

impl<const N: usize> uWrite for String<N> {
type Error = ();
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -28,7 +37,16 @@ mod tests {
}

#[test]
fn test_string() {
fn test_udisplay_string() {
let str_a = String::<32>::from("world");
let str_b = String::<32>::new();
uwrite!(str_b, "Hello {}!", str_a).unwrap();

assert_eq!(str_b, "Hello world!");
}

#[test]
fn test_uwrite_string() {
let a = 123;
let b = Pair { x: 0, y: 1234 };

Expand All @@ -39,14 +57,14 @@ mod tests {
}

#[test]
fn test_string_err() {
fn test_uwrite_string_err() {
let p = Pair { x: 0, y: 1234 };
let mut s = String::<4>::new();
assert!(uwrite!(s, "{:?}", p).is_err());
}

#[test]
fn test_vec() {
fn test_uwrite_vec() {
let a = 123;
let b = Pair { x: 0, y: 1234 };

Expand Down

0 comments on commit cf989c3

Please sign in to comment.