@@ -217,14 +217,16 @@ impl IndexMut<usize> for PageTable {
217
217
impl Index < PageTableIndex > for PageTable {
218
218
type Output = PageTableEntry ;
219
219
220
+ #[ inline]
220
221
fn index ( & self , index : PageTableIndex ) -> & Self :: Output {
221
- & self . entries [ cast :: usize ( u16 :: from ( index) ) ]
222
+ & self . entries [ usize:: from ( index) ]
222
223
}
223
224
}
224
225
225
226
impl IndexMut < PageTableIndex > for PageTable {
227
+ #[ inline]
226
228
fn index_mut ( & mut self , index : PageTableIndex ) -> & mut Self :: Output {
227
- & mut self . entries [ cast :: usize ( u16 :: from ( index) ) ]
229
+ & mut self . entries [ usize:: from ( index) ]
228
230
}
229
231
}
230
232
@@ -237,6 +239,8 @@ impl fmt::Debug for PageTable {
237
239
/// A 9-bit index into a page table.
238
240
///
239
241
/// Can be used to select one of the 512 entries of a page table.
242
+ ///
243
+ /// Guaranteed to only ever contain 0..512.
240
244
#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
241
245
pub struct PageTableIndex ( u16 ) ;
242
246
@@ -246,6 +250,11 @@ impl PageTableIndex {
246
250
assert ! ( usize :: from( index) < ENTRY_COUNT ) ;
247
251
Self ( index)
248
252
}
253
+
254
+ /// Creates a new index from the given `u16`. Throws away bits if the value is >=512.
255
+ pub const fn new_truncate ( index : u16 ) -> Self {
256
+ Self ( index % ENTRY_COUNT as u16 )
257
+ }
249
258
}
250
259
251
260
impl From < PageTableIndex > for u16 {
@@ -266,9 +275,17 @@ impl From<PageTableIndex> for u64 {
266
275
}
267
276
}
268
277
278
+ impl From < PageTableIndex > for usize {
279
+ fn from ( index : PageTableIndex ) -> Self {
280
+ usize:: from ( index. 0 )
281
+ }
282
+ }
283
+
269
284
/// A 12-bit offset into a 4KiB Page.
270
285
///
271
286
/// This type is returned by the `VirtAddr::page_offset` method.
287
+ ///
288
+ /// Guaranteed to only ever contain 0..4096.
272
289
#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
273
290
pub struct PageOffset ( u16 ) ;
274
291
@@ -278,6 +295,11 @@ impl PageOffset {
278
295
assert ! ( offset < ( 1 << 12 ) ) ;
279
296
Self ( offset)
280
297
}
298
+
299
+ /// Creates a new offset from the given `u16`. Throws away bits if the value is >=4096.
300
+ pub const fn new_truncate ( offset : u16 ) -> Self {
301
+ Self ( offset % ( 1 << 12 ) )
302
+ }
281
303
}
282
304
283
305
impl From < PageOffset > for u16 {
@@ -297,3 +319,9 @@ impl From<PageOffset> for u64 {
297
319
u64:: from ( offset. 0 )
298
320
}
299
321
}
322
+
323
+ impl From < PageOffset > for usize {
324
+ fn from ( offset : PageOffset ) -> Self {
325
+ usize:: from ( offset. 0 )
326
+ }
327
+ }
0 commit comments