Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

address types improvements, clippy bullshit #509

Merged
merged 9 commits into from
Jan 11, 2025
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions alloc/src/bump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ impl<const SIZE: usize> Alloc<SIZE> {
}
}

impl<const SIZE: usize> Default for Alloc<SIZE> {
fn default() -> Self {
Self::new()
}
}

unsafe impl<const SIZE: usize> GlobalAlloc for Alloc<SIZE> {
/// # Safety
///
Expand Down
4 changes: 4 additions & 0 deletions bitfield/src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,10 @@ macro_rules! bitfield {
/// Constructs a new instance of `Self` with all bits set to 0.
#[inline]
#[must_use]
// Allow "new without default", as the user may wish to derive
// default or provide their own implementation with different values
// from `new`.
#[allow(clippy::new_without_default)]
$vis const fn new() -> Self {
Self(0)
}
Expand Down
2 changes: 1 addition & 1 deletion bitfield/src/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ mod tests {
prop_assert_bits_eq!(packed, pack_from_src.set_all(0), state);
prop_assert_bits_eq!(pack_from_src.unpack_bits(packed), pack_from_dst.unpack_bits(dst), &state);

let dst = <$Bits>::max_value();
let dst = <$Bits>::MAX;
let packed = pair.pack_from_src(src, dst);
prop_assert_bits_eq!(packed, pack_from_src.set_all(0), state);
prop_assert_bits_eq!(pack_from_src.unpack_bits(packed), pack_from_dst.unpack_bits(dst), &state);
Expand Down
2 changes: 1 addition & 1 deletion hal-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0"
authors = ["Eliza Weisman <[email protected]>", "iximeow <[email protected]>"]
edition = "2021"
license = "MIT"
rust-version = "1.81.0"
rust-version = "1.84.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

Expand Down
83 changes: 74 additions & 9 deletions hal-core/src/addr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use core::{fmt, ops};
use core::{fmt, ops, ptr};

pub trait Address:
Copy
Expand Down Expand Up @@ -76,10 +76,10 @@ pub trait Address:
self.align_down(core::mem::align_of::<T>())
}

/// Offsets this address by `offset`.
/// Offsets this address by `offset` bytes.
///
/// If the specified offset would overflow, this function saturates instead.
fn offset(self, offset: i32) -> Self {
fn offset(self, offset: isize) -> Self {
if offset > 0 {
self + offset as usize
} else {
Expand Down Expand Up @@ -117,19 +117,52 @@ pub trait Address:
self.is_aligned(core::mem::align_of::<T>())
}

/// Converts this address into a const pointer to a value of type `T`.
///
/// # Panics
///
/// - If `self` is not aligned for a `T`-typed value.
#[inline]
#[track_caller]
fn as_ptr<T>(self) -> *const T {
// Some architectures permit unaligned reads, but Rust considers
// dereferencing a pointer that isn't type-aligned to be UB.
assert!(
self.is_aligned_for::<T>(),
"assertion failed: self.is_aligned_for::<{}>();\n\tself={self:?}",
core::any::type_name::<T>(),
);
ptr::with_exposed_provenance(self.as_usize())
}

/// Converts this address into a mutable pointer to a value of type `T`.
///
/// # Panics
///
/// - If `self` is not aligned for a `T`-typed value.
#[inline]
#[track_caller]
fn as_ptr<T>(self) -> *mut T {
fn as_mut_ptr<T>(self) -> *mut T {
// Some architectures permit unaligned reads, but Rust considers
// dereferencing a pointer that isn't type-aligned to be UB.
assert!(
self.is_aligned_for::<T>(),
"assertion failed: self.is_aligned_for::<{}>();\n\tself={self:?}",
core::any::type_name::<T>(),
);
self.as_usize() as *mut T
ptr::with_exposed_provenance_mut(self.as_usize())
}

/// Converts this address into a `Option<NonNull<T>>` from a
/// `VAddr`, returning `None` if the address is null.
///
/// # Panics
///
/// - If `self` is not aligned for a `T`-typed value.
#[inline]
#[track_caller]
fn as_non_null<T>(self) -> Option<ptr::NonNull<T>> {
ptr::NonNull::new(self.as_mut_ptr::<T>())
}
}

Expand Down Expand Up @@ -300,11 +333,11 @@ macro_rules! impl_addrs {
Address::align_down(self, align)
}

/// Offsets this address by `offset`.
/// Offsets this address by `offset` bytes.
///
/// If the specified offset would overflow, this function saturates instead.
#[inline]
pub fn offset(self, offset: i32) -> Self {
pub fn offset(self, offset: isize) -> Self {
Address::offset(self, offset)
}

Expand All @@ -327,13 +360,39 @@ macro_rules! impl_addrs {
Address::is_aligned_for::<T>(self)
}

/// Converts this address into a const pointer to a value of type `T`.
///
/// # Panics
///
/// - If `self` is not aligned for a `T`-typed value.
#[inline]
pub fn as_ptr<T>(self) -> *mut T {
#[track_caller]
pub fn as_ptr<T>(self) -> *const T {
Address::as_ptr(self)
}

/// Converts this address into a mutable pointer to a value of type `T`.
///
/// # Panics
///
/// - If `self` is not aligned for a `T`-typed value.
#[inline]
#[track_caller]
pub fn as_mut_ptr<T>(self) -> *mut T {
Address::as_mut_ptr(self)
}

/// Converts this address into a `Option<NonNull<T>>` from a
/// `VAddr`, returning `None` if the address is null.
///
/// # Panics
///
/// - If `self` is not aligned for a `T`-typed value.
#[inline]
#[track_caller]
pub fn as_non_null<T>(self) -> Option<ptr::NonNull<T>> {
ptr::NonNull::new(self.as_mut_ptr::<T>())
}
}
)+
}
Expand Down Expand Up @@ -392,9 +451,15 @@ impl VAddr {
Self(u)
}

/// Constructs a `VAddr` from a `*const T` pointer, exposing its provenance.
#[inline]
pub fn from_ptr<T: ?Sized>(ptr: *const T) -> Self {
Self::from_usize(ptr.expose_provenance())
}

#[inline]
pub fn of<T: ?Sized>(pointee: &T) -> Self {
Self::from_usize(pointee as *const _ as *const () as usize)
Self::from_ptr(pointee as *const T)
}
}

Expand Down
4 changes: 2 additions & 2 deletions hal-core/src/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ macro_rules! deref_draw_body {
};
}

impl<'lock, D, L> Draw for blocking::MutexGuard<'lock, D, L>
impl<D, L> Draw for blocking::MutexGuard<'_, D, L>
where
D: Draw,
L: blocking::RawMutex,
{
deref_draw_body! {}
}

impl<'draw, D> Draw for &'draw mut D
impl<D> Draw for &'_ mut D
where
D: Draw,
{
Expand Down
2 changes: 1 addition & 1 deletion hal-core/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ enum RegistrationErrorKind {

// === impl CriticalGuard ===

impl<'a, C: Control + ?Sized> Drop for CriticalGuard<'a, C> {
impl<C: Control + ?Sized> Drop for CriticalGuard<'_, C> {
fn drop(&mut self) {
unsafe {
self.ctrl.enable();
Expand Down
8 changes: 4 additions & 4 deletions hal-core/src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ impl<A: Address> Region<A> {
}

pub fn split_front(&mut self, size: usize) -> Option<Self> {
assert!(size <= core::i32::MAX as usize);
assert!(size <= isize::MAX as usize);
if size > self.size {
return None;
}
let base = self.base;
tracing::trace!(size, self.size, "splitting down by");
self.base = self.base.offset(size as i32);
self.base = self.base.offset(size as isize);
self.size -= size;
Some(Self {
base,
Expand All @@ -100,12 +100,12 @@ impl<A: Address> Region<A> {
}

pub fn split_back(&mut self, size: usize) -> Option<Self> {
assert!(size <= core::i32::MAX as usize);
assert!(size <= isize::MAX as usize);
if size >= self.size {
return None;
}
let rem_size = self.size - size;
let base = self.base.offset(size as i32);
let base = self.base.offset(size as isize);
tracing::trace!(
size,
self.size,
Expand Down
4 changes: 2 additions & 2 deletions hal-core/src/mem/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ impl<A: Address, S: Size> Page<A, S> {
/// When calling this method, ensure that the page will not be mutated
/// concurrently, including by user code.
pub unsafe fn as_slice(&self) -> &[u8] {
let start = self.base.as_ptr() as *const u8;
let start = self.base.as_mut_ptr() as *const u8;
slice::from_raw_parts::<u8>(start, self.size.as_usize())
}

Expand All @@ -495,7 +495,7 @@ impl<A: Address, S: Size> Page<A, S> {
/// When calling this method, ensure that the page will not be read or mutated
/// concurrently, including by user code.
pub unsafe fn as_slice_mut(&mut self) -> &mut [u8] {
let start = self.base.as_ptr::<u8>() as *mut _;
let start = self.base.as_mut_ptr::<u8>() as *mut _;
slice::from_raw_parts_mut::<u8>(start, self.size.as_usize())
}
}
Expand Down
2 changes: 1 addition & 1 deletion hal-x86_64/src/cpu/entropy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub fn seed_rng<R: SeedableRng>() -> R {
#[derive(Debug, Copy, Clone)]
pub struct Rdrand(());

#[derive(Debug, Copy, Clone)]
#[derive(Debug, Copy, Clone, Default)]
pub struct PitEntropy(());

// === impl Rdrand ===
Expand Down
2 changes: 1 addition & 1 deletion hal-x86_64/src/cpu/msr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ impl<V> Msr<V> {
options(nomem, nostack, preserves_flags)
);
}
(hi as u64) << 32 | (lo as u64)
((hi as u64) << 32) | (lo as u64)
}

/// Writes the given raw `u64` value to this MSR.
Expand Down
4 changes: 2 additions & 2 deletions hal-x86_64/src/framebuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ where
}
}

impl<'buf, B> Draw for Framebuffer<'buf, B>
impl<B> Draw for Framebuffer<'_, B>
where
B: Deref<Target = [u8]> + DerefMut,
{
Expand Down Expand Up @@ -101,7 +101,7 @@ where

// `Volatile<B>` is only `Debug` if `<B as Deref>::Target: Copy`,
// so we must implement this manually.
impl<'buf, B> fmt::Debug for Framebuffer<'buf, B>
impl<B> fmt::Debug for Framebuffer<'_, B>
where
B: Deref<Target = [u8]>,
{
Expand Down
10 changes: 5 additions & 5 deletions hal-x86_64/src/interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl Controller {
}
}

impl<'a, T> hal_core::interrupt::Context for Context<'a, T> {
impl<T> hal_core::interrupt::Context for Context<'_, T> {
type Registers = Registers;

fn registers(&self) -> &Registers {
Expand All @@ -402,7 +402,7 @@ impl<'a, T> hal_core::interrupt::Context for Context<'a, T> {
}
}

impl<'a> ctx::PageFault for Context<'a, PageFaultCode> {
impl ctx::PageFault for Context<'_, PageFaultCode> {
fn fault_vaddr(&self) -> crate::VAddr {
crate::control_regs::Cr2::read()
}
Expand All @@ -416,7 +416,7 @@ impl<'a> ctx::PageFault for Context<'a, PageFaultCode> {
}
}

impl<'a> ctx::CodeFault for Context<'a, CodeFault<'a>> {
impl ctx::CodeFault for Context<'_, CodeFault<'_>> {
fn is_user_mode(&self) -> bool {
false // TODO(eliza)
}
Expand All @@ -434,13 +434,13 @@ impl<'a> ctx::CodeFault for Context<'a, CodeFault<'a>> {
}
}

impl<'a> Context<'a, ErrorCode> {
impl Context<'_, ErrorCode> {
pub fn error_code(&self) -> ErrorCode {
self.code
}
}

impl<'a> Context<'a, PageFaultCode> {
impl Context<'_, PageFaultCode> {
pub fn page_fault_code(&self) -> PageFaultCode {
self.code
}
Expand Down
5 changes: 2 additions & 3 deletions hal-x86_64/src/interrupt/apic/ioapic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl IoApic {
tracing::debug!("mapped I/O APIC MMIO page!");
}

let registers = unsafe { Volatile::new(&mut *base.as_ptr::<MmioRegisters>()) };
let registers = unsafe { Volatile::new(&mut *base.as_mut_ptr::<MmioRegisters>()) };
let mut ioapic = Self { registers };
tracing::info!(
addr = ?base,
Expand Down Expand Up @@ -440,7 +440,7 @@ impl IoApic {
fn entry_raw(&mut self, register_low: u32) -> RedirectionEntry {
let low = self.read(register_low);
let high = self.read(register_low + 1);
RedirectionEntry::from_bits((high as u64) << 32 | low as u64)
RedirectionEntry::from_bits(((high as u64) << 32) | low as u64)
}

#[inline]
Expand All @@ -453,7 +453,6 @@ impl IoApic {
}

#[must_use]

fn read(&mut self, offset: u32) -> u32 {
self.set_offset(offset);
self.registers.map_mut(|ioapic| &mut ioapic.data).read()
Expand Down
Loading
Loading