Skip to content

Commit

Permalink
derive Default for c enums
Browse files Browse the repository at this point in the history
  • Loading branch information
maia-s committed Jan 2, 2025
1 parent 82609ef commit a30d950
Show file tree
Hide file tree
Showing 36 changed files with 93 additions and 753 deletions.
18 changes: 1 addition & 17 deletions sdl3-sys-gen/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,6 @@ impl Emit for TypeDef {
}

let mut seen_target_dependent = HashSet::new();
let mut first_short_variant_ident = None;

for variant in &e.variants {
ctx.register_sym(
Expand All @@ -1470,9 +1469,6 @@ impl Emit for TypeDef {
short_variant_ident =
&variant_ident[variant_ident.len() - short_variant_ident.len() - 1..];
}
if first_short_variant_ident.is_none() {
first_short_variant_ident = Some(short_variant_ident);
}

let Some(expr) = variant.expr.as_ref().or(next_expr.as_ref()) else {
return Err(ParseErr::new(
Expand Down Expand Up @@ -1560,25 +1556,13 @@ impl Emit for TypeDef {
writeln!(ctx, "#[repr(transparent)]")?;
writeln!(
ctx,
"#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]"
"#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]"
)?;
write!(ctx, "pub struct {enum_ident}(pub ")?;
enum_base_type.emit(ctx)?;
writeln!(ctx, ");")?;
writeln!(ctx)?;

writeln!(ctx, "impl ::core::default::Default for {enum_ident} {{")?;
ctx.increase_indent();
writeln!(ctx, "#[inline(always)]")?;
writeln!(ctx, "fn default() -> Self {{")?;
ctx.increase_indent();
writeln!(ctx, "Self::{}", first_short_variant_ident.unwrap())?;
ctx.decrease_indent();
writeln!(ctx, "}}")?;
ctx.decrease_indent();
writeln!(ctx, "}}")?;
writeln!(ctx)?;

write!(ctx, "impl From<{enum_ident}> for ")?;
enum_base_type.emit(ctx)?;
writeln!(
Expand Down
9 changes: 1 addition & 8 deletions sdl3-sys/src/generated/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,9 @@ pub use SDL_disabled_assert;
/// | [`IGNORE`](SDL_AssertState::IGNORE) | [`SDL_ASSERTION_IGNORE`] | Ignore the assert. |
/// | [`ALWAYS_IGNORE`](SDL_AssertState::ALWAYS_IGNORE) | [`SDL_ASSERTION_ALWAYS_IGNORE`] | Ignore the assert from now on. |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_AssertState(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_AssertState {
#[inline(always)]
fn default() -> Self {
Self::RETRY
}
}

impl From<SDL_AssertState> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_AssertState) -> Self {
Expand Down
18 changes: 2 additions & 16 deletions sdl3-sys/src/generated/asyncio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,9 @@ use super::stdinc::*;
/// | [`WRITE`](SDL_AsyncIOTaskType::WRITE) | [`SDL_ASYNCIO_TASK_WRITE`] | A write operation. |
/// | [`CLOSE`](SDL_AsyncIOTaskType::CLOSE) | [`SDL_ASYNCIO_TASK_CLOSE`] | A close operation. |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_AsyncIOTaskType(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_AsyncIOTaskType {
#[inline(always)]
fn default() -> Self {
Self::READ
}
}

impl From<SDL_AsyncIOTaskType> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_AsyncIOTaskType) -> Self {
Expand Down Expand Up @@ -146,16 +139,9 @@ pub const SDL_ASYNCIO_TASK_CLOSE: SDL_AsyncIOTaskType = SDL_AsyncIOTaskType::CLO
/// | [`FAILURE`](SDL_AsyncIOResult::FAILURE) | [`SDL_ASYNCIO_FAILURE`] | request failed for some reason; check [`SDL_GetError()`]! |
/// | [`CANCELED`](SDL_AsyncIOResult::CANCELED) | [`SDL_ASYNCIO_CANCELED`] | request was canceled before completing. |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_AsyncIOResult(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_AsyncIOResult {
#[inline(always)]
fn default() -> Self {
Self::COMPLETE
}
}

impl From<SDL_AsyncIOResult> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_AsyncIOResult) -> Self {
Expand Down
9 changes: 1 addition & 8 deletions sdl3-sys/src/generated/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,9 @@ pub const SDL_AUDIO_MASK_SIGNED: ::core::primitive::u32 = 32768_u32;
/// | [`S32`](SDL_AudioFormat::S32) | [`SDL_AUDIO_S32`] | (target dependent) |
/// | [`F32`](SDL_AudioFormat::F32) | [`SDL_AUDIO_F32`] | (target dependent) |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_AudioFormat(pub ::core::ffi::c_uint);

impl ::core::default::Default for SDL_AudioFormat {
#[inline(always)]
fn default() -> Self {
Self::UNKNOWN
}
}

impl From<SDL_AudioFormat> for ::core::ffi::c_uint {
#[inline(always)]
fn from(value: SDL_AudioFormat) -> Self {
Expand Down
18 changes: 2 additions & 16 deletions sdl3-sys/src/generated/blendmode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,9 @@ pub const SDL_BLENDMODE_INVALID: SDL_BlendMode = (0x7fffffff as SDL_BlendMode);
/// | [`MINIMUM`](SDL_BlendOperation::MINIMUM) | [`SDL_BLENDOPERATION_MINIMUM`] | min(dst, src) : supported by D3D, OpenGL, OpenGLES, and Vulkan |
/// | [`MAXIMUM`](SDL_BlendOperation::MAXIMUM) | [`SDL_BLENDOPERATION_MAXIMUM`] | max(dst, src) : supported by D3D, OpenGL, OpenGLES, and Vulkan |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_BlendOperation(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_BlendOperation {
#[inline(always)]
fn default() -> Self {
Self::ADD
}
}

impl From<SDL_BlendOperation> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_BlendOperation) -> Self {
Expand Down Expand Up @@ -148,16 +141,9 @@ pub const SDL_BLENDOPERATION_MAXIMUM: SDL_BlendOperation = SDL_BlendOperation::M
/// | [`DST_ALPHA`](SDL_BlendFactor::DST_ALPHA) | [`SDL_BLENDFACTOR_DST_ALPHA`] | dstA, dstA, dstA, dstA |
/// | [`ONE_MINUS_DST_ALPHA`](SDL_BlendFactor::ONE_MINUS_DST_ALPHA) | [`SDL_BLENDFACTOR_ONE_MINUS_DST_ALPHA`] | 1-dstA, 1-dstA, 1-dstA, 1-dstA |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_BlendFactor(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_BlendFactor {
#[inline(always)]
fn default() -> Self {
Self::ZERO
}
}

impl From<SDL_BlendFactor> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_BlendFactor) -> Self {
Expand Down
9 changes: 1 addition & 8 deletions sdl3-sys/src/generated/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,9 @@ pub struct SDL_CameraSpec {
/// | [`FRONT_FACING`](SDL_CameraPosition::FRONT_FACING) | [`SDL_CAMERA_POSITION_FRONT_FACING`] | |
/// | [`BACK_FACING`](SDL_CameraPosition::BACK_FACING) | [`SDL_CAMERA_POSITION_BACK_FACING`] | |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_CameraPosition(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_CameraPosition {
#[inline(always)]
fn default() -> Self {
Self::UNKNOWN
}
}

impl From<SDL_CameraPosition> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_CameraPosition) -> Self {
Expand Down
9 changes: 1 addition & 8 deletions sdl3-sys/src/generated/dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,9 @@ extern "C" {
/// | [`SAVEFILE`](SDL_FileDialogType::SAVEFILE) | [`SDL_FILEDIALOG_SAVEFILE`] | |
/// | [`OPENFOLDER`](SDL_FileDialogType::OPENFOLDER) | [`SDL_FILEDIALOG_OPENFOLDER`] | |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_FileDialogType(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_FileDialogType {
#[inline(always)]
fn default() -> Self {
Self::OPENFILE
}
}

impl From<SDL_FileDialogType> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_FileDialogType) -> Self {
Expand Down
18 changes: 2 additions & 16 deletions sdl3-sys/src/generated/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,9 @@ use super::video::*;
/// | [`LAST`](SDL_EventType::LAST) | [`SDL_EVENT_LAST`] | * This last event is only for bounding internal arrays |
/// | [`ENUM_PADDING`](SDL_EventType::ENUM_PADDING) | [`SDL_EVENT_ENUM_PADDING`] | |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_EventType(pub Uint32);

impl ::core::default::Default for SDL_EventType {
#[inline(always)]
fn default() -> Self {
Self::FIRST
}
}

impl From<SDL_EventType> for Uint32 {
#[inline(always)]
fn from(value: SDL_EventType) -> Self {
Expand Down Expand Up @@ -1968,16 +1961,9 @@ extern "C" {
/// | [`PEEKEVENT`](SDL_EventAction::PEEKEVENT) | [`SDL_PEEKEVENT`] | Check but don't remove events from the queue front. |
/// | [`GETEVENT`](SDL_EventAction::GETEVENT) | [`SDL_GETEVENT`] | Retrieve/remove events from the front of the queue. |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_EventAction(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_EventAction {
#[inline(always)]
fn default() -> Self {
Self::ADDEVENT
}
}

impl From<SDL_EventAction> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_EventAction) -> Self {
Expand Down
27 changes: 3 additions & 24 deletions sdl3-sys/src/generated/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,9 @@ extern "C" {
/// | [`VIDEOS`](SDL_Folder::VIDEOS) | [`SDL_FOLDER_VIDEOS`] | Video files that can be played using a standard video player (mp4, webm...). |
/// | [`COUNT`](SDL_Folder::COUNT) | [`SDL_FOLDER_COUNT`] | Total number of types in this enum, not a folder type by itself. |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_Folder(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_Folder {
#[inline(always)]
fn default() -> Self {
Self::HOME
}
}

impl From<SDL_Folder> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_Folder) -> Self {
Expand Down Expand Up @@ -298,16 +291,9 @@ extern "C" {
/// | [`DIRECTORY`](SDL_PathType::DIRECTORY) | [`SDL_PATHTYPE_DIRECTORY`] | a directory |
/// | [`OTHER`](SDL_PathType::OTHER) | [`SDL_PATHTYPE_OTHER`] | something completely different like a device node (not a symlink, those are always followed) |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_PathType(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_PathType {
#[inline(always)]
fn default() -> Self {
Self::NONE
}
}

impl From<SDL_PathType> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_PathType) -> Self {
Expand Down Expand Up @@ -427,16 +413,9 @@ extern "C" {
/// | [`SUCCESS`](SDL_EnumerationResult::SUCCESS) | [`SDL_ENUM_SUCCESS`] | Value that requests that enumeration stop, successfully. |
/// | [`FAILURE`](SDL_EnumerationResult::FAILURE) | [`SDL_ENUM_FAILURE`] | Value that requests that enumeration stop, as a failure. |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_EnumerationResult(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_EnumerationResult {
#[inline(always)]
fn default() -> Self {
Self::CONTINUE
}
}

impl From<SDL_EnumerationResult> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_EnumerationResult) -> Self {
Expand Down
45 changes: 5 additions & 40 deletions sdl3-sys/src/generated/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,9 @@ use super::sensor::*;
/// | [`NINTENDO_SWITCH_JOYCON_PAIR`](SDL_GamepadType::NINTENDO_SWITCH_JOYCON_PAIR) | [`SDL_GAMEPAD_TYPE_NINTENDO_SWITCH_JOYCON_PAIR`] | |
/// | [`COUNT`](SDL_GamepadType::COUNT) | [`SDL_GAMEPAD_TYPE_COUNT`] | |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_GamepadType(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_GamepadType {
#[inline(always)]
fn default() -> Self {
Self::UNKNOWN
}
}

impl From<SDL_GamepadType> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_GamepadType) -> Self {
Expand Down Expand Up @@ -207,16 +200,9 @@ pub const SDL_GAMEPAD_TYPE_COUNT: SDL_GamepadType = SDL_GamepadType::COUNT;
/// | [`MISC6`](SDL_GamepadButton::MISC6) | [`SDL_GAMEPAD_BUTTON_MISC6`] | Additional button |
/// | [`COUNT`](SDL_GamepadButton::COUNT) | [`SDL_GAMEPAD_BUTTON_COUNT`] | |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_GamepadButton(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_GamepadButton {
#[inline(always)]
fn default() -> Self {
Self::INVALID
}
}

impl From<SDL_GamepadButton> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_GamepadButton) -> Self {
Expand Down Expand Up @@ -377,16 +363,9 @@ pub const SDL_GAMEPAD_BUTTON_COUNT: SDL_GamepadButton = SDL_GamepadButton::COUNT
/// | [`SQUARE`](SDL_GamepadButtonLabel::SQUARE) | [`SDL_GAMEPAD_BUTTON_LABEL_SQUARE`] | |
/// | [`TRIANGLE`](SDL_GamepadButtonLabel::TRIANGLE) | [`SDL_GAMEPAD_BUTTON_LABEL_TRIANGLE`] | |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_GamepadButtonLabel(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_GamepadButtonLabel {
#[inline(always)]
fn default() -> Self {
Self::UNKNOWN
}
}

impl From<SDL_GamepadButtonLabel> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_GamepadButtonLabel) -> Self {
Expand Down Expand Up @@ -464,16 +443,9 @@ pub const SDL_GAMEPAD_BUTTON_LABEL_TRIANGLE: SDL_GamepadButtonLabel =
/// | [`RIGHT_TRIGGER`](SDL_GamepadAxis::RIGHT_TRIGGER) | [`SDL_GAMEPAD_AXIS_RIGHT_TRIGGER`] | |
/// | [`COUNT`](SDL_GamepadAxis::COUNT) | [`SDL_GAMEPAD_AXIS_COUNT`] | |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_GamepadAxis(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_GamepadAxis {
#[inline(always)]
fn default() -> Self {
Self::INVALID
}
}

impl From<SDL_GamepadAxis> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_GamepadAxis) -> Self {
Expand Down Expand Up @@ -538,16 +510,9 @@ pub const SDL_GAMEPAD_AXIS_COUNT: SDL_GamepadAxis = SDL_GamepadAxis::COUNT;
/// | [`AXIS`](SDL_GamepadBindingType::AXIS) | [`SDL_GAMEPAD_BINDTYPE_AXIS`] | |
/// | [`HAT`](SDL_GamepadBindingType::HAT) | [`SDL_GAMEPAD_BINDTYPE_HAT`] | |
#[repr(transparent)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SDL_GamepadBindingType(pub ::core::ffi::c_int);

impl ::core::default::Default for SDL_GamepadBindingType {
#[inline(always)]
fn default() -> Self {
Self::NONE
}
}

impl From<SDL_GamepadBindingType> for ::core::ffi::c_int {
#[inline(always)]
fn from(value: SDL_GamepadBindingType) -> Self {
Expand Down
Loading

0 comments on commit a30d950

Please sign in to comment.