@@ -138,14 +138,14 @@ impl VirtAddr {
138
138
/// Converts the address to a raw pointer.
139
139
#[ cfg( target_pointer_width = "64" ) ]
140
140
#[ inline]
141
- pub fn as_ptr < T > ( self ) -> * const T {
141
+ pub const fn as_ptr < T > ( self ) -> * const T {
142
142
self . as_u64 ( ) as * const T
143
143
}
144
144
145
145
/// Converts the address to a mutable raw pointer.
146
146
#[ cfg( target_pointer_width = "64" ) ]
147
147
#[ inline]
148
- pub fn as_mut_ptr < T > ( self ) -> * mut T {
148
+ pub const fn as_mut_ptr < T > ( self ) -> * mut T {
149
149
self . as_ptr :: < T > ( ) as * mut T
150
150
}
151
151
@@ -420,13 +420,12 @@ impl PhysAddr {
420
420
///
421
421
/// This function panics if a bit in the range 52 to 64 is set.
422
422
#[ inline]
423
- pub fn new ( addr : u64 ) -> PhysAddr {
424
- assert_eq ! (
425
- addr. get_bits( 52 ..64 ) ,
426
- 0 ,
427
- "physical addresses must not have any bits in the range 52 to 64 set"
428
- ) ;
429
- PhysAddr ( addr)
423
+ pub const fn new ( addr : u64 ) -> Self {
424
+ // TODO: Replace with .ok().expect(msg) when that works on stable.
425
+ match Self :: try_new ( addr) {
426
+ Ok ( p) => p,
427
+ Err ( _) => panic ! ( "physical addresses must not have any bits in the range 52 to 64 set" ) ,
428
+ }
430
429
}
431
430
432
431
/// Creates a new physical address, throwing bits 52..64 away.
@@ -449,10 +448,12 @@ impl PhysAddr {
449
448
///
450
449
/// Fails if any bits in the range 52 to 64 are set.
451
450
#[ inline]
452
- pub fn try_new ( addr : u64 ) -> Result < PhysAddr , PhysAddrNotValid > {
453
- match addr. get_bits ( 52 ..64 ) {
454
- 0 => Ok ( PhysAddr ( addr) ) , // address is valid
455
- _ => Err ( PhysAddrNotValid ( addr) ) ,
451
+ pub const fn try_new ( addr : u64 ) -> Result < Self , PhysAddrNotValid > {
452
+ let p = Self :: new_truncate ( addr) ;
453
+ if p. 0 == addr {
454
+ Ok ( p)
455
+ } else {
456
+ Err ( PhysAddrNotValid ( addr) )
456
457
}
457
458
}
458
459
0 commit comments