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

Generate smaller enum types #180

Merged
merged 2 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ctru-rs/src/applets/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct PopUp {
/// Determines whether the Error applet will use word wrapping when displaying a message.
#[doc(alias = "errorType")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
#[repr(u16)]
pub enum WordWrap {
/// Error text is centered in the error applet window and does not use word wrapping.
Disabled = ctru_sys::ERROR_TEXT,
Expand Down
4 changes: 2 additions & 2 deletions ctru-rs/src/applets/mii_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ bitflags! {
///
/// See [`MiiSelector::set_options()`] to learn how to use them.
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct Options: u32 {
pub struct Options: u8 {
/// Show the cancel button.
const ENABLE_CANCEL = ctru_sys::MIISELECTOR_CANCEL;
/// Make guest Miis available to select.
Expand Down Expand Up @@ -135,7 +135,7 @@ impl MiiSelector {
/// ```
#[doc(alias = "miiSelectorSetOptions")]
pub fn set_options(&mut self, options: Options) {
unsafe { ctru_sys::miiSelectorSetOptions(self.config.as_mut(), options.bits()) }
unsafe { ctru_sys::miiSelectorSetOptions(self.config.as_mut(), options.bits().into()) }
}

/// Allowlist a guest Mii based on its index.
Expand Down
30 changes: 15 additions & 15 deletions ctru-rs/src/applets/swkbd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub struct ParentalLock {
/// Can be set with [`SoftwareKeyboard::new()`]
#[doc(alias = "SwkbdType")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
#[repr(u8)]
pub enum Kind {
/// Normal keyboard composed of several pages (QWERTY, accents, symbols, mobile).
Normal = ctru_sys::SWKBD_TYPE_NORMAL,
Expand All @@ -60,7 +60,7 @@ pub enum Kind {
/// The custom callback can be set using [`SoftwareKeyboard::set_filter_callback()`].
#[doc(alias = "SwkbdCallbackResult")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
#[repr(u8)]
pub enum CallbackResult {
/// The callback yields a positive result.
Ok = ctru_sys::SWKBD_CALLBACK_OK,
Expand All @@ -75,7 +75,7 @@ pub enum CallbackResult {
/// Button text and behaviour can be customized with [`SoftwareKeyboard::configure_button()`].
#[doc(alias = "SwkbdButton")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
#[repr(u8)]
pub enum Button {
/// Left button. Usually corresponds to "Cancel".
Left = ctru_sys::SWKBD_BUTTON_LEFT,
Expand All @@ -90,7 +90,7 @@ pub enum Button {
/// Can be set using [`SoftwareKeyboard::set_password_mode()`].
#[doc(alias = "SwkbdPasswordMode")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
#[repr(u8)]
pub enum PasswordMode {
/// The input text will not be concealed.
None = ctru_sys::SWKBD_PASSWORD_NONE,
Expand All @@ -115,7 +115,7 @@ pub enum ButtonConfig {
/// Error returned by an unsuccessful [`SoftwareKeyboard::launch()`].
#[doc(alias = "SwkbdResult")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(i32)]
#[repr(i8)]
pub enum Error {
/// Invalid parameters given to the [`SoftwareKeyboard`] configuration.
InvalidParameters = ctru_sys::SWKBD_INVALID_INPUT,
Expand Down Expand Up @@ -152,7 +152,7 @@ pub enum Error {
/// See [`SoftwareKeyboard::set_validation()`]
#[doc(alias = "SwkbdValidInput")]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u32)]
#[repr(u8)]
pub enum ValidInput {
/// All inputs are accepted.
Anything = ctru_sys::SWKBD_ANYTHING,
Expand All @@ -169,7 +169,7 @@ pub enum ValidInput {
bitflags! {
/// Special features that can be activated via [`SoftwareKeyboard::set_features()`].
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct Features: u32 {
pub struct Features: u16 {
/// Darken top screen while the [`SoftwareKeyboard`] is active.
const DARKEN_TOP_SCREEN = ctru_sys::SWKBD_DARKEN_TOP_SCREEN;
/// Enable predictive input (necessary for Kanji on JPN consoles).
Expand All @@ -192,7 +192,7 @@ bitflags! {
///
/// See [`SoftwareKeyboard::set_validation()`]
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct Filters: u32 {
pub struct Filters: u8 {
/// Disallow the usage of numerical digits.
///
/// The maximum number of digits that are allowed to be used while this filter is active
Expand Down Expand Up @@ -299,7 +299,7 @@ impl SoftwareKeyboard {
/// # }
#[doc(alias = "swkbdSetFeatures")]
pub fn set_features(&mut self, features: Features) {
unsafe { ctru_sys::swkbdSetFeatures(self.state.as_mut(), features.bits()) }
unsafe { ctru_sys::swkbdSetFeatures(self.state.as_mut(), features.bits().into()) }
}

/// Configure input validation for this keyboard.
Expand All @@ -323,7 +323,7 @@ impl SoftwareKeyboard {
/// # }
pub fn set_validation(&mut self, validation: ValidInput, filters: Filters) {
self.state.valid_input = validation.into();
self.state.filter_flags = filters.bits();
self.state.filter_flags = filters.bits().into();
}

/// Configure a custom filtering function to validate the input.
Expand Down Expand Up @@ -469,7 +469,7 @@ impl SoftwareKeyboard {
#[doc(alias = "swkbdSetPasswordMode")]
pub fn set_password_mode(&mut self, mode: PasswordMode) {
unsafe {
ctru_sys::swkbdSetPasswordMode(self.state.as_mut(), mode.into());
ctru_sys::swkbdSetPasswordMode(self.state.as_mut(), mode as _);
}
}

Expand Down Expand Up @@ -720,9 +720,9 @@ impl SoftwareKeyboard {
}

if self.filter_callback.is_some() {
swkbd.filter_flags |= SWKBD_FILTER_CALLBACK;
swkbd.filter_flags |= u32::from(SWKBD_FILTER_CALLBACK);
} else {
swkbd.filter_flags &= !SWKBD_FILTER_CALLBACK;
swkbd.filter_flags &= !u32::from(SWKBD_FILTER_CALLBACK);
}

// Launch swkbd
Expand Down Expand Up @@ -846,7 +846,7 @@ impl SoftwareKeyboard {

let _ = unsafe {
APT_SendParameter(
envGetAptAppId(),
envGetAptAppId() as _,
sender,
APTCMD_MESSAGE,
(swkbd as *mut SwkbdState).cast(),
Expand Down Expand Up @@ -876,7 +876,7 @@ impl ParentalLock {
unsafe {
let mut state = Box::<SwkbdState>::default();
ctru_sys::swkbdInit(state.as_mut(), Kind::Normal.into(), 1, -1);
ctru_sys::swkbdSetFeatures(state.as_mut(), ctru_sys::SWKBD_PARENTAL);
ctru_sys::swkbdSetFeatures(state.as_mut(), ctru_sys::SWKBD_PARENTAL.into());
Self { state }
}
}
Expand Down
13 changes: 6 additions & 7 deletions ctru-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ impl Try for ResultCode {
// Wait timeouts aren't counted as "failures" in libctru, but an unfinished task means unsafety for us.
// Luckily all summary cases are for system failures (except RS_SUCCESS).
// I don't know if there are any cases in libctru where a Result holds a "failing" summary but a "success" code, so we'll just check for both.
if ctru_sys::R_FAILED(self.0) || ctru_sys::R_SUMMARY(self.0) != ctru_sys::RS_SUCCESS as i32
{
if ctru_sys::R_FAILED(self.0) || ctru_sys::R_SUMMARY(self.0) != ctru_sys::RS_SUCCESS {
ControlFlow::Break(self.into())
} else {
ControlFlow::Continue(())
Expand Down Expand Up @@ -119,7 +118,7 @@ impl Error {
/// Check if the error is a timeout.
pub fn is_timeout(&self) -> bool {
match *self {
Error::Os(code) => R_DESCRIPTION(code) == ctru_sys::RD_TIMEOUT as ctru_sys::Result,
Error::Os(code) => R_DESCRIPTION(code) == ctru_sys::RD_TIMEOUT,
_ => false,
}
}
Expand Down Expand Up @@ -193,7 +192,7 @@ fn result_code_level_str(result: ctru_sys::Result) -> Cow<'static, str> {
RL_TEMPORARY, RL_USAGE,
};

Cow::Borrowed(match R_LEVEL(result) as u32 {
Cow::Borrowed(match R_LEVEL(result) {
RL_SUCCESS => "success",
RL_INFO => "info",
RL_FATAL => "fatal",
Expand All @@ -214,7 +213,7 @@ fn result_code_summary_str(result: ctru_sys::Result) -> Cow<'static, str> {
RS_WOULDBLOCK, RS_WRONGARG,
};

Cow::Borrowed(match R_SUMMARY(result) as u32 {
Cow::Borrowed(match R_SUMMARY(result) {
RS_SUCCESS => "success",
RS_NOP => "nop",
RS_WOULDBLOCK => "would_block",
Expand Down Expand Up @@ -242,7 +241,7 @@ fn result_code_description_str(result: ctru_sys::Result) -> Cow<'static, str> {
RD_SUCCESS, RD_TIMEOUT, RD_TOO_LARGE,
};

Cow::Borrowed(match R_DESCRIPTION(result) as u32 {
Cow::Borrowed(match R_DESCRIPTION(result) {
RD_SUCCESS => "success",
RD_INVALID_RESULT_VALUE => "invalid_result_value",
RD_TIMEOUT => "timeout",
Expand Down Expand Up @@ -292,7 +291,7 @@ fn result_code_module_str(result: ctru_sys::Result) -> Cow<'static, str> {
RM_TCB, RM_TEST, RM_UART, RM_UDS, RM_UPDATER, RM_UTIL, RM_VCTL, RM_WEB_BROWSER,
};

Cow::Borrowed(match R_MODULE(result) as u32 {
Cow::Borrowed(match R_MODULE(result) {
RM_COMMON => "common",
RM_KERNEL => "kernel",
RM_UTIL => "util",
Expand Down
8 changes: 4 additions & 4 deletions ctru-rs/src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn kernel_version() -> Version {
/// ```
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
#[repr(u32)]
#[repr(u8)]
pub enum MemRegion {
/// All memory regions.
All = ctru_sys::MEMREGION_ALL,
Expand All @@ -85,21 +85,21 @@ pub enum MemRegion {
impl MemRegion {
/// Get the total size of this memory region, in bytes.
pub fn size(&self) -> usize {
unsafe { ctru_sys::osGetMemRegionSize(*self as u32) }
unsafe { ctru_sys::osGetMemRegionSize(*self as u8) }
.try_into()
.unwrap()
}

/// Get the number of bytes used within this memory region.
pub fn used(&self) -> usize {
unsafe { ctru_sys::osGetMemRegionUsed(*self as u32) }
unsafe { ctru_sys::osGetMemRegionUsed(*self as u8) }
.try_into()
.unwrap()
}

/// Get the number of bytes free within this memory region.
pub fn free(&self) -> usize {
unsafe { ctru_sys::osGetMemRegionFree(*self as u32) }
unsafe { ctru_sys::osGetMemRegionFree(*self as u8) }
.try_into()
.unwrap()
}
Expand Down
4 changes: 3 additions & 1 deletion ctru-rs/src/services/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ impl<'a> Chainloader<'a> {
#[doc(alias = "aptIsChainload")]
pub fn is_set(&self) -> bool {
// static funtion not exported
unsafe { (ctru_sys::envGetSystemRunFlags() & ctru_sys::RUNFLAG_APTCHAINLOAD) != 0 }
unsafe {
(ctru_sys::envGetSystemRunFlags() & u32::from(ctru_sys::RUNFLAG_APTCHAINLOAD)) != 0
}
}

/// Clears the chainloader state.
Expand Down
Loading
Loading