@@ -77,7 +77,7 @@ pub unsafe trait Flash {
77
77
fn size ( & self ) -> Result < u32 , Error > ;
78
78
79
79
/// Attempts to read `out.len()` bytes starting at `offset`.
80
- fn read ( & self , offset : Ptr , out : & mut [ u8 ] ) -> Result < ( ) , Error > ;
80
+ fn read ( & self , offset : u32 , out : & mut [ u8 ] ) -> Result < ( ) , Error > ;
81
81
82
82
/// Attempts to perform a "direct read" of the given `Region`.
83
83
///
@@ -100,7 +100,7 @@ pub unsafe trait Flash {
100
100
/// # use manticore::hardware::flash::*;
101
101
/// # struct Foo;
102
102
/// # impl Foo {
103
- /// # fn read(&self, offset: Ptr , out: &mut [u8]) -> Result<(), Error> {
103
+ /// # fn read(&self, offset: u32 , out: &mut [u8]) -> Result<(), Error> {
104
104
/// # Ok(())
105
105
/// # }
106
106
/// fn read_direct<'a: 'c, 'b: 'c, 'c>(
@@ -110,7 +110,7 @@ pub unsafe trait Flash {
110
110
/// align: usize,
111
111
/// ) -> Result<&'c [u8], Error> {
112
112
/// let mut buf = arena.alloc_aligned(region.len as usize, align)?;
113
- /// self.read(region.ptr , &mut buf)?;
113
+ /// self.read(region.offset , &mut buf)?;
114
114
/// Ok(buf)
115
115
/// }
116
116
/// # }
@@ -135,7 +135,7 @@ pub unsafe trait Flash {
135
135
/// Implementations are, as an optimization, permitted to assume that
136
136
/// writes will be serial and localized, so as to minimize clearing
137
137
/// operations on flash hardware.
138
- fn program ( & mut self , offset : Ptr , buf : & [ u8 ] ) -> Result < ( ) , Error > ;
138
+ fn program ( & mut self , offset : u32 , buf : & [ u8 ] ) -> Result < ( ) , Error > ;
139
139
140
140
/// Flushes any pending `program()` operations.
141
141
fn flush ( & mut self ) -> Result < ( ) , Error > {
@@ -151,7 +151,7 @@ unsafe impl<F: Flash> Flash for &F {
151
151
}
152
152
153
153
#[ inline]
154
- fn read ( & self , offset : Ptr , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
154
+ fn read ( & self , offset : u32 , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
155
155
F :: read ( self , offset, out)
156
156
}
157
157
@@ -166,7 +166,7 @@ unsafe impl<F: Flash> Flash for &F {
166
166
}
167
167
168
168
#[ inline]
169
- fn program ( & mut self , _: Ptr , _: & [ u8 ] ) -> Result < ( ) , Error > {
169
+ fn program ( & mut self , _: u32 , _: & [ u8 ] ) -> Result < ( ) , Error > {
170
170
Err ( Error :: Locked )
171
171
}
172
172
@@ -183,7 +183,7 @@ unsafe impl<F: Flash> Flash for &mut F {
183
183
}
184
184
185
185
#[ inline]
186
- fn read ( & self , offset : Ptr , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
186
+ fn read ( & self , offset : u32 , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
187
187
F :: read ( self , offset, out)
188
188
}
189
189
@@ -198,7 +198,7 @@ unsafe impl<F: Flash> Flash for &mut F {
198
198
}
199
199
200
200
#[ inline]
201
- fn program ( & mut self , offset : Ptr , buf : & [ u8 ] ) -> Result < ( ) , Error > {
201
+ fn program ( & mut self , offset : u32 , buf : & [ u8 ] ) -> Result < ( ) , Error > {
202
202
F :: program ( self , offset, buf)
203
203
}
204
204
@@ -218,7 +218,7 @@ pub trait FlashExt<'flash> {
218
218
/// See [`ArenaExt::alloc()`](../../mem/arena/trait.ArenaExt.html#tymethod.alloc).
219
219
fn read_object < ' b : ' c , ' c , T > (
220
220
self ,
221
- offset : Ptr ,
221
+ offset : u32 ,
222
222
arena : & ' b dyn Arena ,
223
223
) -> Result < & ' c T , Error >
224
224
where
@@ -230,7 +230,7 @@ pub trait FlashExt<'flash> {
230
230
/// See [`ArenaExt::alloc_slice()`](../../mem/arena/trait.ArenaExt.html#tymethod.alloc_slice).
231
231
fn read_slice < ' b : ' c , ' c , T > (
232
232
self ,
233
- offset : Ptr ,
233
+ offset : u32 ,
234
234
n : usize ,
235
235
arena : & ' b dyn Arena ,
236
236
) -> Result < & ' c [ T ] , Error >
@@ -242,15 +242,15 @@ pub trait FlashExt<'flash> {
242
242
impl < ' flash , F : Flash > FlashExt < ' flash > for & ' flash F {
243
243
fn read_object < ' b : ' c , ' c , T > (
244
244
self ,
245
- offset : Ptr ,
245
+ offset : u32 ,
246
246
arena : & ' b dyn Arena ,
247
247
) -> Result < & ' c T , Error >
248
248
where
249
249
' flash : ' c ,
250
250
T : AsBytes + FromBytes + Copy ,
251
251
{
252
252
let bytes = self . read_direct (
253
- Region :: new ( offset. address , mem:: size_of :: < T > ( ) as u32 ) ,
253
+ Region :: new ( offset, mem:: size_of :: < T > ( ) as u32 ) ,
254
254
arena,
255
255
mem:: align_of :: < T > ( ) ,
256
256
) ?;
@@ -262,7 +262,7 @@ impl<'flash, F: Flash> FlashExt<'flash> for &'flash F {
262
262
263
263
fn read_slice < ' b : ' c , ' c , T > (
264
264
self ,
265
- offset : Ptr ,
265
+ offset : u32 ,
266
266
n : usize ,
267
267
arena : & ' b dyn Arena ,
268
268
) -> Result < & ' c [ T ] , Error >
@@ -273,7 +273,7 @@ impl<'flash, F: Flash> FlashExt<'flash> for &'flash F {
273
273
let bytes_requested =
274
274
stride_of :: < T > ( ) . checked_mul ( n) . ok_or ( OutOfMemory ) ?;
275
275
let bytes = self . read_direct (
276
- Region :: new ( offset. address , bytes_requested as u32 ) ,
276
+ Region :: new ( offset, bytes_requested as u32 ) ,
277
277
arena,
278
278
mem:: align_of :: < T > ( ) ,
279
279
) ?;
@@ -303,9 +303,9 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
303
303
}
304
304
305
305
#[ inline]
306
- fn read ( & self , offset : Ptr , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
306
+ fn read ( & self , offset : u32 , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
307
307
out. copy_from_slice ( self . read_direct (
308
- Region :: new ( offset. address , out. len ( ) as u32 ) ,
308
+ Region :: new ( offset, out. len ( ) as u32 ) ,
309
309
& OutOfMemory ,
310
310
1 ,
311
311
) ?) ;
@@ -318,7 +318,7 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
318
318
arena : & ' b dyn Arena ,
319
319
align : usize ,
320
320
) -> Result < & ' c [ u8 ] , Error > {
321
- let start = region. ptr . address as usize ;
321
+ let start = region. offset as usize ;
322
322
let end = start
323
323
. checked_add ( region. len as usize )
324
324
. ok_or ( Error :: OutOfRange ) ?;
@@ -337,7 +337,7 @@ unsafe impl<Bytes: AsRef<[u8]>> Flash for Ram<Bytes> {
337
337
Ok ( buf)
338
338
}
339
339
340
- fn program ( & mut self , _: Ptr , _: & [ u8 ] ) -> Result < ( ) , Error > {
340
+ fn program ( & mut self , _: u32 , _: & [ u8 ] ) -> Result < ( ) , Error > {
341
341
return Err ( Error :: Locked ) ;
342
342
}
343
343
}
@@ -361,9 +361,9 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
361
361
}
362
362
363
363
#[ inline]
364
- fn read ( & self , offset : Ptr , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
364
+ fn read ( & self , offset : u32 , out : & mut [ u8 ] ) -> Result < ( ) , Error > {
365
365
out. copy_from_slice ( self . read_direct (
366
- Region :: new ( offset. address , out. len ( ) as u32 ) ,
366
+ Region :: new ( offset, out. len ( ) as u32 ) ,
367
367
& OutOfMemory ,
368
368
1 ,
369
369
) ?) ;
@@ -376,7 +376,7 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
376
376
arena : & ' b dyn Arena ,
377
377
align : usize ,
378
378
) -> Result < & ' c [ u8 ] , Error > {
379
- let start = region. ptr . address as usize ;
379
+ let start = region. offset as usize ;
380
380
let end = start
381
381
. checked_add ( region. len as usize )
382
382
. ok_or ( Error :: OutOfRange ) ?;
@@ -395,8 +395,8 @@ unsafe impl<Bytes: AsRef<[u8]> + AsMut<[u8]>> Flash for RamMut<Bytes> {
395
395
Ok ( buf)
396
396
}
397
397
398
- fn program ( & mut self , offset : Ptr , buf : & [ u8 ] ) -> Result < ( ) , Error > {
399
- let start = offset. address as usize ;
398
+ fn program ( & mut self , offset : u32 , buf : & [ u8 ] ) -> Result < ( ) , Error > {
399
+ let start = offset as usize ;
400
400
let end = start. checked_add ( buf. len ( ) ) . ok_or ( Error :: OutOfRange ) ?;
401
401
if end > self . 0 . as_ref ( ) . len ( ) {
402
402
return Err ( Error :: OutOfRange ) ;
@@ -451,7 +451,7 @@ impl<F: Flash> FlashIo<F> {
451
451
/// Adapts this `FlashIo` to only read bytes out from the selected
452
452
/// `Region`.
453
453
pub fn reslice ( & mut self , region : Region ) {
454
- self . cursor = region. ptr . address ;
454
+ self . cursor = region. offset ;
455
455
self . len = region. end ( ) ;
456
456
}
457
457
}
@@ -463,7 +463,7 @@ impl<F: Flash> io::Read for FlashIo<F> {
463
463
}
464
464
465
465
self . flash
466
- . read ( Ptr :: new ( self . cursor ) , out)
466
+ . read ( self . cursor , out)
467
467
. map_err ( |_| io:: Error :: Internal ) ?;
468
468
self . cursor += out. len ( ) as u32 ;
469
469
Ok ( ( ) )
@@ -482,7 +482,7 @@ impl<F: Flash> Iterator for FlashIo<F> {
482
482
}
483
483
484
484
let mut byte = [ 0 ] ;
485
- if let Err ( e) = self . flash . read ( Ptr :: new ( self . cursor ) , & mut byte) {
485
+ if let Err ( e) = self . flash . read ( self . cursor , & mut byte) {
486
486
return Some ( Err ( e) ) ;
487
487
}
488
488
self . cursor += 1 ;
@@ -497,61 +497,34 @@ impl<F: Flash> io::Write for FlashIo<F> {
497
497
}
498
498
499
499
self . flash
500
- . program ( Ptr :: new ( self . cursor ) , buf)
500
+ . program ( self . cursor , buf)
501
501
. map_err ( |_| io:: Error :: Internal ) ?;
502
502
self . cursor += buf. len ( ) as u32 ;
503
503
Ok ( ( ) )
504
504
}
505
505
}
506
506
507
- /// An abstract pointer into a [`Flash`] type.
507
+ /// A region within a [`Flash`] type.
508
508
///
509
- /// A `Ptr ` needs to be used in conjunction with a [`Flash`]
510
- /// implementation to be read from or written to .
509
+ /// A `Region ` needs to be interpreted with respect to a [`Flash`]
510
+ /// implementation; it is otherwise a dumb pointer-length pair .
511
511
///
512
512
/// [`Flash`]: trait.Flash.html
513
513
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , AsBytes , FromBytes ) ]
514
- #[ repr( transparent) ]
515
- #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
516
- pub struct Ptr {
517
- /// The abstract address of this pointer.
518
- pub address : u32 ,
519
- }
520
-
521
- impl Ptr {
522
- /// Convenience method for creating a `Ptr` without having to use
523
- /// a struct literal.
524
- pub const fn new ( address : u32 ) -> Self {
525
- Self { address }
526
- }
527
- }
528
-
529
- /// A region within a [`Flash`] type.
530
- ///
531
- /// Much like a [`Ptr`], a `Region` needs to be interpreted with
532
- /// respect to a [`Flash`] implementation.
533
- ///
534
- /// [`Flash`]: trait.Flash.html
535
- /// [`Ptr`]: struct.Ptr.html
536
- #[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , AsBytes , FromBytes ) ]
537
514
#[ repr( C ) ]
538
515
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
539
516
pub struct Region {
540
517
/// The base pointer for this slice.
541
- #[ cfg_attr( feature = "serde" , serde( flatten) ) ]
542
- pub ptr : Ptr ,
518
+ pub offset : u32 ,
543
519
/// The length of the slice, in bytes.
544
520
pub len : u32 ,
545
521
}
546
522
547
523
impl Region {
548
524
/// Convenience method for creating a `Region` without having to use
549
525
/// a struct literal.
550
- pub const fn new ( ptr : u32 , len : u32 ) -> Self {
551
- Self {
552
- ptr : Ptr :: new ( ptr) ,
553
- len,
554
- }
526
+ pub const fn new ( offset : u32 , len : u32 ) -> Self {
527
+ Self { offset, len }
555
528
}
556
529
557
530
/// Returns a `Region` big enough to hold a `T`.
@@ -569,7 +542,7 @@ impl Region {
569
542
570
543
/// Returns the end address of `self`, pointing one past the end of it.
571
544
pub fn end ( self ) -> u32 {
572
- self . ptr . address . saturating_add ( self . len )
545
+ self . offset . saturating_add ( self . len )
573
546
}
574
547
575
548
/// Returns a new `Region` that comes immediately after `self`, with the
@@ -583,14 +556,11 @@ impl Region {
583
556
///
584
557
/// Returns `None` if `sub` is not a subregion of `self`.
585
558
pub fn subregion ( self , sub : Region ) -> Option < Self > {
586
- if sub. len . saturating_add ( sub. ptr . address ) > self . len {
559
+ if sub. len . saturating_add ( sub. offset ) > self . len {
587
560
return None ;
588
561
}
589
562
590
- Some ( Region :: new (
591
- self . ptr . address . checked_add ( sub. ptr . address ) ?,
592
- sub. len ,
593
- ) )
563
+ Some ( Region :: new ( self . offset . checked_add ( sub. offset ) ?, sub. len ) )
594
564
}
595
565
596
566
/// Contracts `self` by dropping the first `n` bytes.
@@ -599,7 +569,7 @@ impl Region {
599
569
/// occurs.
600
570
pub fn skip ( self , n : u32 ) -> Option < Self > {
601
571
Some ( Region :: new (
602
- self . ptr . address . checked_add ( n) ?,
572
+ self . offset . checked_add ( n) ?,
603
573
self . len . checked_sub ( n) ?,
604
574
) )
605
575
}
@@ -608,6 +578,6 @@ impl Region {
608
578
///
609
579
/// Returns `None` if `n` is greater than `self.len`.
610
580
pub fn take ( self , n : u32 ) -> Option < Self > {
611
- Some ( Region :: new ( self . ptr . address , self . len . checked_sub ( n) ?) )
581
+ Some ( Region :: new ( self . offset , self . len . checked_sub ( n) ?) )
612
582
}
613
583
}
0 commit comments