Skip to content

Commit bbe46e9

Browse files
committed
Add rustfmt file & format source code
1 parent fa8d78f commit bbe46e9

File tree

4 files changed

+50
-48
lines changed

4 files changed

+50
-48
lines changed

examples/printer/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
//!
32
//! Universal printer type.
43
//!
@@ -27,6 +26,7 @@ impl Printer {
2726
#[cfg(not(target_arch = "avr"))]
2827
println!("{}", s);
2928
}
29+
3030
pub fn print(&mut self, c: char) {
3131
#[cfg(target_arch = "avr")]
3232
ufmt::uwrite!(&mut self.0, "{}", c).void_unwrap();
@@ -35,4 +35,3 @@ impl Printer {
3535
print!("{}", c);
3636
}
3737
}
38-

examples/uno-serial.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@
3535
#![cfg_attr(target_arch = "avr", no_std)]
3636
#![no_main]
3737

38-
// Import a halting panic implementation for AVR
39-
#[cfg(target_arch = "avr")]
40-
use panic_halt as _;
4138

42-
// Our library to be actually test here!
4339
use avr_progmem::progmem;
40+
#[cfg(target_arch = "avr")]
41+
use panic_halt as _; // halting panic implementation for AVR
4442

4543

4644
// The length of the below data block.
@@ -92,13 +90,12 @@ fn main() -> ! {
9290
// Loop through the entire `TEXT` and print it char-by-char
9391
let mut idx = 0;
9492
loop {
95-
9693
printer.print(TEXT.load_at(idx) as char);
9794

9895
idx += 1;
9996

10097
if idx >= TEXT_LEN {
101-
break
98+
break;
10299
}
103100
}
104101

rustfmt.toml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Custom Formatting Configuration
2+
# See: https://github.com/rust-lang/rustfmt/blob/master/Configurations.md
3+
# Notice, you need a nightly toolchain for correctly formatting
4+
5+
hard_tabs = true # Use true tabs, no spaces for indentation
6+
edition = "2018" # not necessary for `cargo fmt`, but seems reasonable to have
7+
merge_derives = false # Do not combine separate #[derives]
8+
newline_style = "Unix"
9+
use_field_init_shorthand = true # Enforce using shorthand
10+
11+
# Unstable stuff, which is nice to have:
12+
unstable_features = true # Opt-in into nightly features
13+
blank_lines_upper_bound = 3 # Allow up to 3 blank lines between items
14+
match_block_trailing_comma = true # Always add a comma in matches
15+
combine_control_expr = false # Put control expressions in block format
16+
force_multiline_blocks = true # Use blocks for multiline code
17+
format_macro_matchers = true # (try) formatting macro_rules
18+
format_strings = true # Line break long strings
19+
imports_layout = "Vertical" # Each item on a separate line
20+
imports_granularity = "Item" # Use a single import per module
21+
group_imports = "StdExternalCrate" # Group by std, external, self
22+
reorder_impl_items = true # Put types first in impl blocks
23+
struct_lit_single_line = false # Each field on a separate line
24+

src/lib.rs

+22-40
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
// We don't need anything from std, and on AVR there is no std anyway.
32
#![no_std]
4-
3+
//
54
// We need inline assembly for the `lpm` instruction.
65
#![feature(llvm_asm)]
7-
6+
//
87
// We need const generics, however the `const_generics` feature is reported as
98
// incomplete, thus we actually use the `min_const_generics` feature, which is
109
// sufficient for us. However, min_const_generics in turn fails to work with
@@ -13,7 +12,6 @@
1312
#![cfg_attr(doc, feature(const_generics))]
1413
#![cfg_attr(not(doc), feature(min_const_generics))]
1514

16-
1715
//!
1816
//! Progmem utilities for the AVR architectures.
1917
//!
@@ -181,9 +179,9 @@
181179
//!
182180
183181

182+
use core::convert::TryInto;
184183
use core::mem::size_of;
185184
use core::mem::MaybeUninit;
186-
use core::convert::TryInto;
187185

188186
use cfg_if::cfg_if;
189187

@@ -293,9 +291,7 @@ impl<T: Copy> ProgMem<T> {
293291
// This is safe, because the invariant of this struct demands that
294292
// this value (i.e. self and thus also its inner value) are stored
295293
// in the progmem domain, which is what `read_value` requires from us.
296-
unsafe {
297-
read_value(p_addr)
298-
}
294+
unsafe { read_value(p_addr) }
299295
}
300296

301297
/// Return the raw pointer to the inner value.
@@ -310,8 +306,7 @@ impl<T: Copy> ProgMem<T> {
310306
}
311307

312308
/// Utilities to work with an array in progmem.
313-
impl<T: Copy, const N: usize> ProgMem<[T;N]> {
314-
309+
impl<T: Copy, const N: usize> ProgMem<[T; N]> {
315310
/// Load a single element from the inner array.
316311
///
317312
/// This method is analog to a slice indexing `self.inner[idx]`, so the
@@ -340,9 +335,7 @@ impl<T: Copy, const N: usize> ProgMem<[T;N]> {
340335
//
341336
// Also notice that the slice-indexing above gives us a bounds check.
342337
//
343-
unsafe {
344-
read_value(addr)
345-
}
338+
unsafe { read_value(addr) }
346339
}
347340

348341
/// Loads a sub array from the inner array.
@@ -369,25 +362,23 @@ impl<T: Copy, const N: usize> ProgMem<[T;N]> {
369362
/// However, this is currently just a implementation limitation, which may
370363
/// be lifted in the future.
371364
///
372-
pub fn load_sub_array<const M: usize>(&self, start_idx: usize) -> [T;M] {
365+
pub fn load_sub_array<const M: usize>(&self, start_idx: usize) -> [T; M] {
373366
assert!(M <= N);
374367

375368
// Make sure that we convert from &[T] to &[T;M] without constructing
376369
// an actual [T;M], because we MAY NOT LOAD THE DATA YET!
377370
// Also notice, that this sub-slicing dose ensure that the bound are
378371
// correct.
379-
let slice: &[T] = &self.0[start_idx..(start_idx+M)];
380-
let array: &[T;M] = slice.try_into().unwrap();
372+
let slice: &[T] = &self.0[start_idx..(start_idx + M)];
373+
let array: &[T; M] = slice.try_into().unwrap();
381374

382375
// This is safe, because the invariant of this struct demands that
383376
// this value (i.e. self and thus also its inner value) are stored
384377
// in the progmem domain, which is what `read_value` requires from us.
385378
//
386379
// Also notice that the sub-slicing above gives us a bounds check.
387380
//
388-
unsafe {
389-
read_value(array)
390-
}
381+
unsafe { read_value(array) }
391382
}
392383
}
393384

@@ -586,8 +577,9 @@ pub unsafe fn read_byte(p_addr: *const u8) -> u8 {
586577
///
587578
#[allow(dead_code)]
588579
unsafe fn read_byte_loop_raw<T>(p_addr: *const T, out: *mut T, len: u8)
589-
where T: Sized + Copy {
590-
580+
where
581+
T: Sized + Copy,
582+
{
591583
// Convert to byte pointers
592584
let p_addr_bytes = p_addr as *const u8;
593585
let out_bytes = out as *mut u8;
@@ -635,14 +627,13 @@ unsafe fn read_byte_loop_raw<T>(p_addr: *const T, out: *mut T, len: u8)
635627
/// `core::ptr::copy` and therefore the pointers must be aligned.
636628
///
637629
unsafe fn read_asm_loop_raw<T>(p_addr: *const T, out: *mut T, len: u8) {
638-
639630
// Here are the general requirements essentially required by the AVR-impl
640631
// However, assume, the non-AVR version is only used in tests, it makes a
641632
// lot of sens to ensure the AVR requirements are held up.
642633

643634
// Loop head check, just return for zero iterations
644635
if len == 0 || size_of::<T>() == 0 {
645-
return
636+
return;
646637
}
647638

648639
// Get size in bytes of T
@@ -658,7 +649,7 @@ unsafe fn read_asm_loop_raw<T>(p_addr: *const T, out: *mut T, len: u8) {
658649
let size_bytes = size_bytes as u8;
659650

660651

661-
cfg_if!{
652+
cfg_if! {
662653
if #[cfg(target_arch = "avr")] {
663654
// Only addresses below the 64 KiB limit are supported
664655
// Apparently this is of no concern for architectures with true
@@ -748,9 +739,10 @@ unsafe fn read_asm_loop_raw<T>(p_addr: *const T, out: *mut T, len: u8) {
748739
/// must be aligned.
749740
///
750741
unsafe fn read_value_raw<T>(p_addr: *const T, out: *mut T, len: u8)
751-
where T: Sized + Copy {
752-
753-
cfg_if!{
742+
where
743+
T: Sized + Copy,
744+
{
745+
cfg_if! {
754746
if #[cfg(feature = "lpm-asm-loop")] {
755747
read_asm_loop_raw(p_addr, out, len)
756748
} else {
@@ -934,8 +926,9 @@ pub unsafe fn read_slice(p: &[u8], out: &mut [u8]) {
934926
///
935927
#[cfg_attr(feature = "dev", inline(never))]
936928
pub unsafe fn read_value<T>(p_addr: *const T) -> T
937-
where T: Sized + Copy {
938-
929+
where
930+
T: Sized + Copy,
931+
{
939932
// The use of an MaybeUninit allows us to correctly allocate the space
940933
// required to hold one `T`, whereas we correctly comunicate that it is
941934
// uninitialized to the compiler.
@@ -964,14 +957,3 @@ pub unsafe fn read_value<T>(p_addr: *const T) -> T
964957
// initialized, and this call is sound.
965958
buffer.assume_init()
966959
}
967-
968-
969-
970-
971-
#[cfg(test)]
972-
mod tests {
973-
#[test]
974-
fn it_works() {
975-
assert_eq!(2 + 2, 4);
976-
}
977-
}

0 commit comments

Comments
 (0)