Skip to content

Commit 673fca7

Browse files
committed
Add ufmt::uDisplay impl on ByteString
1 parent 98f8b9c commit 673fca7

File tree

5 files changed

+61
-5
lines changed

5 files changed

+61
-5
lines changed

Cargo.toml

+11-5
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,31 @@ opt-level = "s"
2929

3030

3131
[features]
32-
default = ["lpm-asm-loop"]
32+
default = ["lpm-asm-loop", "ufmt"]
3333
# Enables the use of the `lpm r0, Z+` in an assembly loop, instead of the plain
3434
# `lpm` instruction with a Rust loop.
3535
lpm-asm-loop = []
3636
# Enables some tweak to ease debugging, should not be use in production
3737
dev = []
3838

39-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
40-
4139
[dependencies]
4240
cfg-if = "0.1.10"
4341

42+
[dependencies.ufmt]
43+
version = "0.1.0"
44+
optional = true
45+
4446

4547
[dev-dependencies]
4648
panic-halt = "0.2.0"
4749
ufmt = "0.1.0"
4850

51+
[dev-dependencies.void]
52+
version = "1.0"
53+
default-features = false
54+
55+
4956
[dev-dependencies.arduino-uno]
5057
# It is not yet available via crates.io
5158
git = "https://github.com/Rahix/avr-hal.git"
52-
rev = "0c6cf1675c2724354f1adeaeee69992acd371e80"
53-
59+
rev = "0c6cf1675c2724354f1adeaeee69992acd371e80"

examples/printer/mod.rs

+24
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,27 @@ impl Printer {
3535
print!("{}", c);
3636
}
3737
}
38+
39+
impl ufmt::uWrite for Printer {
40+
type Error = void::Void;
41+
42+
fn write_str(&mut self, s: &str) -> Result<(), Self::Error> {
43+
#[cfg(target_arch = "avr")]
44+
ufmt::uwrite!(&mut self.0, "{}", s).void_unwrap();
45+
46+
#[cfg(not(target_arch = "avr"))]
47+
println!("{}", s);
48+
49+
Ok(())
50+
}
51+
52+
fn write_char(&mut self, c: char) -> Result<(), Self::Error> {
53+
#[cfg(target_arch = "avr")]
54+
ufmt::uwrite!(&mut self.0, "{}", c).void_unwrap();
55+
56+
#[cfg(not(target_arch = "avr"))]
57+
print!("{}", c);
58+
59+
Ok(())
60+
}
61+
}

examples/uno-string.rs

+4
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ fn main() -> ! {
9494
printer.println(avr_progmem::progmem_str!("Just a lone literal progmem str"));
9595
printer.println(avr_progmem::progmem_str!("And another one"));
9696

97+
// Using the ufmt impl
98+
#[cfg(feature = "ufmt")]
99+
ufmt::uwriteln!(&mut printer, "{}\r", UNICODE_TEXT.load()).unwrap();
100+
97101
// Print some final lines
98102
printer.println("");
99103
printer.println("--------------------------");

src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
//
99
// For string support, we need to convert from slice to array in const context.
1010
#![cfg_attr(not(doc), feature(const_raw_ptr_deref))]
11+
//
12+
// Allows to document required crate features on items
13+
#![feature(doc_cfg)]
1114

1215
//!
1316
//! Progmem utilities for the AVR architectures.

src/string.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::fmt;
12
use core::ops::Deref;
23

34

@@ -84,6 +85,24 @@ impl<const N: usize> Deref for ByteString<N> {
8485
}
8586
}
8687

88+
impl<const N: usize> fmt::Display for ByteString<N> {
89+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
90+
write!(fmt, "{}", self.deref())
91+
}
92+
}
93+
94+
#[cfg(any(feature = "ufmt", doc))]
95+
#[doc(cfg(feature = "ufmt"))]
96+
impl<const N: usize> ufmt::uDisplay for ByteString<N> {
97+
fn fmt<W: ?Sized>(&self, fmt: &mut ufmt::Formatter<W>) -> Result<(), W::Error>
98+
where
99+
W: ufmt::uWrite,
100+
{
101+
ufmt::uwrite!(fmt, "{}", self.deref())
102+
}
103+
}
104+
105+
87106
/// Define a string in progmem
88107
///
89108
/// This is a short-cut macro to create an ad-hoc static storing the given

0 commit comments

Comments
 (0)