Skip to content

port: read/write now not mutable self #549

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

Closed
wants to merge 1 commit into from
Closed

Conversation

fox0
Copy link

@fox0 fox0 commented May 11, 2025

No description provided.

@Freax13
Copy link
Member

Freax13 commented May 17, 2025

Hi there, sorry for the delay in reviewing this!

I'm not sure how to feel about this. I think requiring a unique reference to the port is more often useful than not, so I'm a bit hesitant to remove that. If your primary goal is to use a port stored in a const, note that this is already possible today by just making a local copy:

const DATA_PORT: PortGeneric<u8, ReadWriteAccess> = Port::<u8>::new(0x0060);
const COMMAND_REGISTER: PortGeneric<u8, WriteOnlyAccess> = PortWriteOnly::<u8>::new(0x0064);
const STATUS_REGISTER: PortGeneric<u8, ReadOnlyAccess> = PortReadOnly::<u8>::new(0x0064);

fn foo() {
    let mut data_port = DATA_PORT;
    let mut command_register = COMMAND_REGISTER;
    let umt status_register = STATUS_REGISTER;
    unsafe {
        let _ = data_port.read();
        data_port.write(0x42);
        command_register.write(0x42);
        let _ = status_register.read();
    }
}

Thoughts?

@fox0
Copy link
Author

fox0 commented May 17, 2025

Made a wrapper over PortGeneric for my private project.

use core::marker::PhantomData;
use x86_64::instructions::port::{
    PortReadAccess, PortWriteAccess, ReadOnlyAccess, ReadWriteAccess, WriteOnlyAccess,
};
use x86_64::structures::port::{PortRead, PortWrite};

pub struct PortGeneric<T, A> {
    port: u16,
    phantom: PhantomData<(T, A)>,
}

pub type Port<T> = PortGeneric<T, ReadWriteAccess>;
pub type PortReadOnly<T> = PortGeneric<T, ReadOnlyAccess>;
pub type PortWriteOnly<T> = PortGeneric<T, WriteOnlyAccess>;

impl<T, A> PortGeneric<T, A> {
    #[inline]
    pub const fn new(port: u16) -> PortGeneric<T, A> {
        PortGeneric {
            port,
            phantom: PhantomData,
        }
    }
}

impl<T: PortRead, A: PortReadAccess> PortGeneric<T, A> {
    #[inline]
    #[must_use]
    pub unsafe fn read(&self) -> T {
        unsafe { T::read_from_port(self.port) }
    }
}

impl<T: PortWrite, A: PortWriteAccess> PortGeneric<T, A> {
    #[inline]
    pub unsafe fn write(&self, value: T) {
        unsafe { T::write_to_port(self.port, value) }
    }
}

const DATA_PORT: PortGeneric<u8, ReadWriteAccess> = Port::<u8>::new(0x0060);
const COMMAND_REGISTER: PortGeneric<u8, WriteOnlyAccess> = PortWriteOnly::<u8>::new(0x0064);
const STATUS_REGISTER: PortGeneric<u8, ReadOnlyAccess> = PortReadOnly::<u8>::new(0x0064);

#[deny(const_item_mutation)]
fn foo() {
    unsafe {
        let _ = DATA_PORT.read();
        DATA_PORT.write(0x42);
        COMMAND_REGISTER.write(0x42);
        let _ = STATUS_REGISTER.read();
    }
}

@fox0 fox0 closed this May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants