Skip to content
This repository was archived by the owner on Apr 6, 2025. It is now read-only.
This repository was archived by the owner on Apr 6, 2025. It is now read-only.

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

Description

@flier

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);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions