Skip to content

Commit 47b28cb

Browse files
committed
Fixes clippy
1 parent bf0153a commit 47b28cb

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/lib.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub trait Kernel: MappedKernel {
9696
fn get<O: Offset>(self, off: O) -> O::Ops {
9797
let addr = unsafe { self.addr().add(off.get()) };
9898

99-
<O::Ops as OffsetOps>::new(addr)
99+
unsafe { <O::Ops as OffsetOps>::new(addr) }
100100
}
101101

102102
/// # Safety
@@ -295,7 +295,9 @@ pub trait Offset: Copy {
295295

296296
/// Contains possible operations on an item at the [`Offset`].
297297
pub trait OffsetOps: Copy {
298-
fn new(addr: *const u8) -> Self;
298+
/// # Safety
299+
/// `addr` must be valid for this [`OffsetOps`] to operate on.
300+
unsafe fn new(addr: *const u8) -> Self;
299301
}
300302

301303
/// Offset of an immutable static value in the kernel.
@@ -338,7 +340,7 @@ impl<T> Offset for Static<T> {
338340
pub struct ImmutableOps<T>(*const T);
339341

340342
impl<T> OffsetOps for ImmutableOps<T> {
341-
fn new(addr: *const u8) -> Self {
343+
unsafe fn new(addr: *const u8) -> Self {
342344
Self(addr.cast())
343345
}
344346
}
@@ -429,7 +431,7 @@ impl<T> Clone for MutableOps<T> {
429431
impl<T> Copy for MutableOps<T> {}
430432

431433
impl<T> OffsetOps for MutableOps<T> {
432-
fn new(addr: *const u8) -> Self {
434+
unsafe fn new(addr: *const u8) -> Self {
433435
Self(addr.cast_mut().cast())
434436
}
435437
}
@@ -475,7 +477,8 @@ pub struct FunctionOps<T> {
475477

476478
impl<T: KernelFn> FunctionOps<T> {
477479
pub fn as_ptr(self) -> T {
478-
T::from_addr(self.addr)
480+
// SAFETY: self.addr is guarantee to be valid by Function::new.
481+
unsafe { T::from_addr(self.addr) }
479482
}
480483
}
481484

@@ -488,7 +491,7 @@ impl<T> Clone for FunctionOps<T> {
488491
impl<T> Copy for FunctionOps<T> {}
489492

490493
impl<T> OffsetOps for FunctionOps<T> {
491-
fn new(addr: *const u8) -> Self {
494+
unsafe fn new(addr: *const u8) -> Self {
492495
Self {
493496
addr,
494497
phantom: PhantomData,
@@ -498,11 +501,13 @@ impl<T> OffsetOps for FunctionOps<T> {
498501

499502
/// Provides method to cast kernel address into a function pointer.
500503
pub trait KernelFn: Copy {
501-
fn from_addr(addr: *const u8) -> Self;
504+
/// # Safety
505+
/// `addr` must be the first instruction of this function.
506+
unsafe fn from_addr(addr: *const u8) -> Self;
502507
}
503508

504509
impl<R, A1> KernelFn for extern "C" fn(A1, ...) -> R {
505-
fn from_addr(addr: *const u8) -> Self {
510+
unsafe fn from_addr(addr: *const u8) -> Self {
506511
unsafe { transmute(addr) }
507512
}
508513
}

0 commit comments

Comments
 (0)