1
-
2
1
// We don't need anything from std, and on AVR there is no std anyway.
3
2
#![ no_std]
4
-
3
+ //
5
4
// We need inline assembly for the `lpm` instruction.
6
5
#![ feature( llvm_asm) ]
7
-
6
+ //
8
7
// We need const generics, however the `const_generics` feature is reported as
9
8
// incomplete, thus we actually use the `min_const_generics` feature, which is
10
9
// sufficient for us. However, min_const_generics in turn fails to work with
13
12
#![ cfg_attr( doc, feature( const_generics) ) ]
14
13
#![ cfg_attr( not( doc) , feature( min_const_generics) ) ]
15
14
16
-
17
15
//!
18
16
//! Progmem utilities for the AVR architectures.
19
17
//!
181
179
//!
182
180
183
181
182
+ use core:: convert:: TryInto ;
184
183
use core:: mem:: size_of;
185
184
use core:: mem:: MaybeUninit ;
186
- use core:: convert:: TryInto ;
187
185
188
186
use cfg_if:: cfg_if;
189
187
@@ -293,9 +291,7 @@ impl<T: Copy> ProgMem<T> {
293
291
// This is safe, because the invariant of this struct demands that
294
292
// this value (i.e. self and thus also its inner value) are stored
295
293
// 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) }
299
295
}
300
296
301
297
/// Return the raw pointer to the inner value.
@@ -310,8 +306,7 @@ impl<T: Copy> ProgMem<T> {
310
306
}
311
307
312
308
/// 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 ] > {
315
310
/// Load a single element from the inner array.
316
311
///
317
312
/// 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]> {
340
335
//
341
336
// Also notice that the slice-indexing above gives us a bounds check.
342
337
//
343
- unsafe {
344
- read_value ( addr)
345
- }
338
+ unsafe { read_value ( addr) }
346
339
}
347
340
348
341
/// Loads a sub array from the inner array.
@@ -369,25 +362,23 @@ impl<T: Copy, const N: usize> ProgMem<[T;N]> {
369
362
/// However, this is currently just a implementation limitation, which may
370
363
/// be lifted in the future.
371
364
///
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 ] {
373
366
assert ! ( M <= N ) ;
374
367
375
368
// Make sure that we convert from &[T] to &[T;M] without constructing
376
369
// an actual [T;M], because we MAY NOT LOAD THE DATA YET!
377
370
// Also notice, that this sub-slicing dose ensure that the bound are
378
371
// 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 ( ) ;
381
374
382
375
// This is safe, because the invariant of this struct demands that
383
376
// this value (i.e. self and thus also its inner value) are stored
384
377
// in the progmem domain, which is what `read_value` requires from us.
385
378
//
386
379
// Also notice that the sub-slicing above gives us a bounds check.
387
380
//
388
- unsafe {
389
- read_value ( array)
390
- }
381
+ unsafe { read_value ( array) }
391
382
}
392
383
}
393
384
@@ -586,8 +577,9 @@ pub unsafe fn read_byte(p_addr: *const u8) -> u8 {
586
577
///
587
578
#[ allow( dead_code) ]
588
579
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
+ {
591
583
// Convert to byte pointers
592
584
let p_addr_bytes = p_addr as * const u8 ;
593
585
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)
635
627
/// `core::ptr::copy` and therefore the pointers must be aligned.
636
628
///
637
629
unsafe fn read_asm_loop_raw < T > ( p_addr : * const T , out : * mut T , len : u8 ) {
638
-
639
630
// Here are the general requirements essentially required by the AVR-impl
640
631
// However, assume, the non-AVR version is only used in tests, it makes a
641
632
// lot of sens to ensure the AVR requirements are held up.
642
633
643
634
// Loop head check, just return for zero iterations
644
635
if len == 0 || size_of :: < T > ( ) == 0 {
645
- return
636
+ return ;
646
637
}
647
638
648
639
// 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) {
658
649
let size_bytes = size_bytes as u8 ;
659
650
660
651
661
- cfg_if ! {
652
+ cfg_if ! {
662
653
if #[ cfg( target_arch = "avr" ) ] {
663
654
// Only addresses below the 64 KiB limit are supported
664
655
// 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) {
748
739
/// must be aligned.
749
740
///
750
741
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 ! {
754
746
if #[ cfg( feature = "lpm-asm-loop" ) ] {
755
747
read_asm_loop_raw( p_addr, out, len)
756
748
} else {
@@ -934,8 +926,9 @@ pub unsafe fn read_slice(p: &[u8], out: &mut [u8]) {
934
926
///
935
927
#[ cfg_attr( feature = "dev" , inline( never) ) ]
936
928
pub unsafe fn read_value < T > ( p_addr : * const T ) -> T
937
- where T : Sized + Copy {
938
-
929
+ where
930
+ T : Sized + Copy ,
931
+ {
939
932
// The use of an MaybeUninit allows us to correctly allocate the space
940
933
// required to hold one `T`, whereas we correctly comunicate that it is
941
934
// uninitialized to the compiler.
@@ -964,14 +957,3 @@ pub unsafe fn read_value<T>(p_addr: *const T) -> T
964
957
// initialized, and this call is sound.
965
958
buffer. assume_init ( )
966
959
}
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