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