Skip to content

Commit 0a69f1d

Browse files
authored
Merge pull request #124 from m-ou-se/no-cast
Remove the `cast` dependency.
2 parents 0334cbc + 22917f8 commit 0a69f1d

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,5 @@ edition = "2018"
2929
bit_field = "0.9.0"
3030
bitflags = "1.0.4"
3131

32-
[dependencies.cast]
33-
version = "0.2.2"
34-
default-features = false
35-
3632
[features]
3733
deny-warnings = []

src/addr.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,19 @@ impl VirtAddr {
8888
}
8989

9090
/// Creates a virtual address from the given pointer
91+
// cfg(target_pointer_width = "32") is only here for backwards
92+
// compatibility: Earlier versions of this crate did not have any `cfg()`
93+
// on this function. At least for 32- and 64-bit we know the `as u64` cast
94+
// doesn't truncate.
95+
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
9196
pub fn from_ptr<T>(ptr: *const T) -> Self {
92-
Self::new(cast::u64(ptr as usize))
97+
Self::new(ptr as u64)
9398
}
9499

95100
/// Converts the address to a raw pointer.
96101
#[cfg(target_pointer_width = "64")]
97102
pub fn as_ptr<T>(self) -> *const T {
98-
cast::usize(self.as_u64()) as *const T
103+
self.as_u64() as *const T
99104
}
100105

101106
/// Converts the address to a mutable raw pointer.
@@ -181,14 +186,14 @@ impl AddAssign<u64> for VirtAddr {
181186
impl Add<usize> for VirtAddr {
182187
type Output = Self;
183188
fn add(self, rhs: usize) -> Self::Output {
184-
self + cast::u64(rhs)
189+
self + rhs as u64
185190
}
186191
}
187192

188193
#[cfg(target_pointer_width = "64")]
189194
impl AddAssign<usize> for VirtAddr {
190195
fn add_assign(&mut self, rhs: usize) {
191-
self.add_assign(cast::u64(rhs))
196+
self.add_assign(rhs as u64)
192197
}
193198
}
194199

@@ -209,14 +214,14 @@ impl SubAssign<u64> for VirtAddr {
209214
impl Sub<usize> for VirtAddr {
210215
type Output = Self;
211216
fn sub(self, rhs: usize) -> Self::Output {
212-
self - cast::u64(rhs)
217+
self - rhs as u64
213218
}
214219
}
215220

216221
#[cfg(target_pointer_width = "64")]
217222
impl SubAssign<usize> for VirtAddr {
218223
fn sub_assign(&mut self, rhs: usize) {
219-
self.sub_assign(cast::u64(rhs))
224+
self.sub_assign(rhs as u64)
220225
}
221226
}
222227

@@ -347,14 +352,14 @@ impl AddAssign<u64> for PhysAddr {
347352
impl Add<usize> for PhysAddr {
348353
type Output = Self;
349354
fn add(self, rhs: usize) -> Self::Output {
350-
self + cast::u64(rhs)
355+
self + rhs as u64
351356
}
352357
}
353358

354359
#[cfg(target_pointer_width = "64")]
355360
impl AddAssign<usize> for PhysAddr {
356361
fn add_assign(&mut self, rhs: usize) {
357-
self.add_assign(cast::u64(rhs))
362+
self.add_assign(rhs as u64)
358363
}
359364
}
360365

@@ -375,14 +380,14 @@ impl SubAssign<u64> for PhysAddr {
375380
impl Sub<usize> for PhysAddr {
376381
type Output = Self;
377382
fn sub(self, rhs: usize) -> Self::Output {
378-
self - cast::u64(rhs)
383+
self - rhs as u64
379384
}
380385
}
381386

382387
#[cfg(target_pointer_width = "64")]
383388
impl SubAssign<usize> for PhysAddr {
384389
fn sub_assign(&mut self, rhs: usize) {
385-
self.sub_assign(cast::u64(rhs))
390+
self.sub_assign(rhs as u64)
386391
}
387392
}
388393

0 commit comments

Comments
 (0)