madsmtm / objc2

Bindings to Apple's frameworks in Rust
https://docs.rs/objc2/
MIT License
281 stars 35 forks source link

Put enum constants on the enum itself #580

Closed madsmtm closed 3 months ago

madsmtm commented 3 months ago

This required two large rewrites of header-translator's internals, as emitting the correct Rust for a given enum constant was tricky, but now we're here!

This should help a lot with uncluttering the top-level namespace for each framework.

// Generated before
pub type AXHearingDeviceEar = NSInteger;
pub const AXHearingDeviceEarNone: AXHearingDeviceEar = 0;
pub const AXHearingDeviceEarLeft: AXHearingDeviceEar = 1 << 1;
pub const AXHearingDeviceEarRight: AXHearingDeviceEar = 1 << 2;
pub const AXHearingDeviceEarBoth: AXHearingDeviceEar = AXHearingDeviceEarLeft | AXHearingDeviceEarRight;

// Generated now
pub struct AXHearingDeviceEar(pub NSInteger);
impl AXHearingDeviceEar {
    #[doc(alias = "AXHearingDeviceEarNone")]
    pub const None: Self = Self(0);
    #[doc(alias = "AXHearingDeviceEarLeft")]
    pub const Left: Self = Self(1 << 1);
    #[doc(alias = "AXHearingDeviceEarRight")]
    pub const Right: Self = Self(1 << 2);
    #[doc(alias = "AXHearingDeviceEarBoth")]
    pub const Both: Self = Self(AXHearingDeviceEar::Left.0 | AXHearingDeviceEar::Right.0);
}

Part of https://github.com/madsmtm/objc2/issues/310, though more work here is definitely needed.