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

Hide the raw type with private(?) traits #3

Open
flier opened this issue Feb 19, 2024 · 1 comment
Open

Hide the raw type with private(?) traits #3

flier opened this issue Feb 19, 2024 · 1 comment

Comments

@flier
Copy link

flier commented Feb 19, 2024

There are a lot of wrapper types that expose internal types, which is unnecessary for most usage scenarios.

#[repr(transparent)]
pub struct FlagAction(xed_flag_action_t);

impl FlagAction {
    pub fn from_ref(raw: &xed_flag_action_t) -> &Self {
        // SAFETY: SimpleFlag is #[repr(transparent)]
        unsafe { std::mem::transmute(raw) }
    }

    pub fn from_raw(raw: xed_flag_action_t) -> Self {
        Self(raw)
    }

    pub fn into_raw(self) -> xed_flag_action_t {
        self.0
    }

    pub fn as_raw(&self) -> &xed_flag_action_t {
        &self.0
    }

    pub fn as_raw_mut(&mut self) -> &mut xed_flag_action_t {
        &mut self.0
    }
}

Maybe we can extract some traits to hide it like AsPtr?

pub trait AsRaw {
    type CType;

    fn as_raw(&self) -> &Self::CType;
}

impl AsRaw for FlagAction {
    type  CType = xed_flag_action_t;

    fn as_raw(&self) -> &Self::CType {
        &self.0
    }
}

And we could add a produce macros to generate those implementations

#[repr(transparent)]
#[derive(FromRaw, IntoRaw, AsRaw, AsRawMut)]
pub struct FlagAction(xed_flag_action_t);
flier added a commit to flier/xed that referenced this issue Feb 19, 2024
@Phantomical
Copy link
Member

I think I mentioned all this in the review over in #5 but I'll add it here for posterity :)

Getting rid of the copy-paste here is definitely a good idea! I just think that doing it via traits is the wrong approach. Rather, using a macro to take care of the definitions means that we don't have to copy paste anymore but still keeps these methods declared with the type in the documentation and when people are using autocomplete to find them.

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

No branches or pull requests

2 participants