Rename ReturnType to ReturnVariant. This was discussed on the comments at https://github.com/tock/libtock-rs/pull/267. ReturnVariant is more consistent with Rust's use of enum "variants".
diff --git a/core/platform/src/command_return.rs b/core/platform/src/command_return.rs index 7b4abf3..7fa7302 100644 --- a/core/platform/src/command_return.rs +++ b/core/platform/src/command_return.rs
@@ -1,10 +1,10 @@ -use crate::{return_type, ErrorCode, ReturnType}; +use crate::{return_variant, ErrorCode, ReturnVariant}; /// The response type from `command`. Can represent a successful value or a /// failure. #[derive(Clone, Copy)] pub struct CommandReturn { - pub(crate) return_type: ReturnType, + pub(crate) return_variant: ReturnVariant, // r1, r2, and r3 should only contain 32-bit values. However, these are // converted directly from usizes returned by RawSyscalls::four_arg_syscall. // To avoid casting twice (both when converting to a Command Return and when @@ -25,59 +25,59 @@ // } else if let Some(error_code) = command_return.get_failure() { // // ... // } else { - // // Incorrect return type + // // Incorrect return variant // } /// Returns true if this CommandReturn is of type Failure. Note that this /// does not return true for other failure types, such as Failure with u32. pub fn is_failure(&self) -> bool { - self.return_type == return_type::FAILURE + self.return_variant == return_variant::FAILURE } /// Returns true if this CommandReturn is of type Failure with u32. pub fn is_failure_u32(&self) -> bool { - self.return_type == return_type::FAILURE_U32 + self.return_variant == return_variant::FAILURE_U32 } /// Returns true if this CommandReturn is of type Failure with 2 u32. pub fn is_failure_2_u32(&self) -> bool { - self.return_type == return_type::FAILURE_2_U32 + self.return_variant == return_variant::FAILURE_2_U32 } /// Returns true if this CommandReturn is of type Failure with u64. pub fn is_failure_u64(&self) -> bool { - self.return_type == return_type::FAILURE_U64 + self.return_variant == return_variant::FAILURE_U64 } /// Returns true if this CommandReturn is of type Success. Note that this /// does not return true for other success types, such as Success with u32. pub fn is_success(&self) -> bool { - self.return_type == return_type::SUCCESS + self.return_variant == return_variant::SUCCESS } /// Returns true if this CommandReturn is of type Success with u32. pub fn is_success_u32(&self) -> bool { - self.return_type == return_type::SUCCESS_U32 + self.return_variant == return_variant::SUCCESS_U32 } /// Returns true if this CommandReturn is of type Success with 2 u32. pub fn is_success_2_u32(&self) -> bool { - self.return_type == return_type::SUCCESS_2_U32 + self.return_variant == return_variant::SUCCESS_2_U32 } /// Returns true if this CommandReturn is of type Success with u64. pub fn is_success_u64(&self) -> bool { - self.return_type == return_type::SUCCESS_U64 + self.return_variant == return_variant::SUCCESS_U64 } /// Returns true if this CommandReturn is of type Success with 3 u32. pub fn is_success_3_u32(&self) -> bool { - self.return_type == return_type::SUCCESS_3_U32 + self.return_variant == return_variant::SUCCESS_3_U32 } /// Returns true if this CommandReturn is of type Success with u32 and u64. pub fn is_success_u32_u64(&self) -> bool { - self.return_type == return_type::SUCCESS_U32_U64 + self.return_variant == return_variant::SUCCESS_U32_U64 } /// Returns the error code if this CommandReturn is of type Failure. @@ -156,8 +156,8 @@ Some((self.r1 as u32, self.r2 as u64 + ((self.r3 as u64) << 32))) } - /// Returns the return type of this command. - pub fn return_type(&self) -> ReturnType { - self.return_type + /// Returns the return variant of this command. + pub fn return_variant(&self) -> ReturnVariant { + self.return_variant } }
diff --git a/core/platform/src/command_return_tests.rs b/core/platform/src/command_return_tests.rs index ccde359..1712f71 100644 --- a/core/platform/src/command_return_tests.rs +++ b/core/platform/src/command_return_tests.rs
@@ -1,9 +1,9 @@ -use crate::{error_code, return_type, CommandReturn}; +use crate::{error_code, return_variant, CommandReturn}; #[test] fn failure() { let command_return = CommandReturn { - return_type: return_type::FAILURE, + return_variant: return_variant::FAILURE, r1: error_code::RESERVE.into(), r2: 1002, r3: 1003, @@ -27,13 +27,13 @@ assert_eq!(command_return.get_success_u64(), None); assert_eq!(command_return.get_success_3_u32(), None); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::FAILURE); + assert_eq!(command_return.return_variant(), return_variant::FAILURE); } #[test] fn failure_u32() { let command_return = CommandReturn { - return_type: return_type::FAILURE_U32, + return_variant: return_variant::FAILURE_U32, r1: error_code::OFF.into(), r2: 1002, r3: 1003, @@ -60,13 +60,13 @@ assert_eq!(command_return.get_success_u64(), None); assert_eq!(command_return.get_success_3_u32(), None); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::FAILURE_U32); + assert_eq!(command_return.return_variant(), return_variant::FAILURE_U32); } #[test] fn failure_2_u32() { let command_return = CommandReturn { - return_type: return_type::FAILURE_2_U32, + return_variant: return_variant::FAILURE_2_U32, r1: error_code::ALREADY.into(), r2: 1002, r3: 1003, @@ -93,13 +93,16 @@ assert_eq!(command_return.get_success_u64(), None); assert_eq!(command_return.get_success_3_u32(), None); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::FAILURE_2_U32); + assert_eq!( + command_return.return_variant(), + return_variant::FAILURE_2_U32 + ); } #[test] fn failure_u64() { let command_return = CommandReturn { - return_type: return_type::FAILURE_U64, + return_variant: return_variant::FAILURE_U64, r1: error_code::BUSY.into(), r2: 0x00001002, r3: 0x00001003, @@ -126,13 +129,13 @@ assert_eq!(command_return.get_success_u64(), None); assert_eq!(command_return.get_success_3_u32(), None); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::FAILURE_U64); + assert_eq!(command_return.return_variant(), return_variant::FAILURE_U64); } #[test] fn success() { let command_return = CommandReturn { - return_type: return_type::SUCCESS, + return_variant: return_variant::SUCCESS, r1: 1001, r2: 1002, r3: 1003, @@ -156,13 +159,13 @@ assert_eq!(command_return.get_success_u64(), None); assert_eq!(command_return.get_success_3_u32(), None); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::SUCCESS); + assert_eq!(command_return.return_variant(), return_variant::SUCCESS); } #[test] fn success_u32() { let command_return = CommandReturn { - return_type: return_type::SUCCESS_U32, + return_variant: return_variant::SUCCESS_U32, r1: 1001, r2: 1002, r3: 1003, @@ -186,13 +189,13 @@ assert_eq!(command_return.get_success_u64(), None); assert_eq!(command_return.get_success_3_u32(), None); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::SUCCESS_U32); + assert_eq!(command_return.return_variant(), return_variant::SUCCESS_U32); } #[test] fn success_2_u32() { let command_return = CommandReturn { - return_type: return_type::SUCCESS_2_U32, + return_variant: return_variant::SUCCESS_2_U32, r1: 1001, r2: 1002, r3: 1003, @@ -216,13 +219,16 @@ assert_eq!(command_return.get_success_u64(), None); assert_eq!(command_return.get_success_3_u32(), None); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::SUCCESS_2_U32); + assert_eq!( + command_return.return_variant(), + return_variant::SUCCESS_2_U32 + ); } #[test] fn success_u64() { let command_return = CommandReturn { - return_type: return_type::SUCCESS_U64, + return_variant: return_variant::SUCCESS_U64, r1: 0x00001001, r2: 0x00001002, r3: 1003, @@ -246,13 +252,13 @@ assert_eq!(command_return.get_success_u64(), Some(0x00001002_00001001)); assert_eq!(command_return.get_success_3_u32(), None); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::SUCCESS_U64); + assert_eq!(command_return.return_variant(), return_variant::SUCCESS_U64); } #[test] fn success_3_u32() { let command_return = CommandReturn { - return_type: return_type::SUCCESS_3_U32, + return_variant: return_variant::SUCCESS_3_U32, r1: 1001, r2: 1002, r3: 1003, @@ -276,13 +282,16 @@ assert_eq!(command_return.get_success_u64(), None); assert_eq!(command_return.get_success_3_u32(), Some((1001, 1002, 1003))); assert_eq!(command_return.get_success_u32_u64(), None); - assert_eq!(command_return.return_type(), return_type::SUCCESS_3_U32); + assert_eq!( + command_return.return_variant(), + return_variant::SUCCESS_3_U32 + ); } #[test] fn success_u32_u64() { let command_return = CommandReturn { - return_type: return_type::SUCCESS_U32_U64, + return_variant: return_variant::SUCCESS_U32_U64, r1: 1001, r2: 0x00001002, r3: 0x00001003, @@ -309,5 +318,8 @@ command_return.get_success_u32_u64(), Some((1001, 0x00001003_00001002)) ); - assert_eq!(command_return.return_type(), return_type::SUCCESS_U32_U64); + assert_eq!( + command_return.return_variant(), + return_variant::SUCCESS_U32_U64 + ); }
diff --git a/core/platform/src/lib.rs b/core/platform/src/lib.rs index 460c999..45021ca 100644 --- a/core/platform/src/lib.rs +++ b/core/platform/src/lib.rs
@@ -4,7 +4,7 @@ mod command_return; pub mod error_code; mod raw_syscalls; -pub mod return_type; +pub mod return_variant; mod syscalls; mod syscalls_impl; @@ -12,7 +12,7 @@ pub use command_return::CommandReturn; pub use error_code::ErrorCode; pub use raw_syscalls::{OneArgMemop, RawSyscalls, YieldType, ZeroArgMemop}; -pub use return_type::ReturnType; +pub use return_variant::ReturnVariant; pub use syscalls::Syscalls; #[cfg(test)]
diff --git a/core/platform/src/return_type.rs b/core/platform/src/return_type.rs deleted file mode 100644 index 84fb8e9..0000000 --- a/core/platform/src/return_type.rs +++ /dev/null
@@ -1,32 +0,0 @@ -/// `ReturnType` describes what value type the kernel has returned. -// ReturnType is not an enum so that it can be converted from a u32 for free. -// TODO: derive(Debug) is currently only enabled for test builds, which is -// necessary so it can be used in assert_eq!. We should develop a lighter-weight -// Debug implementation and see if it is small enough to enable on non-Debug -// builds. -#[cfg_attr(test, derive(Debug))] -#[derive(Clone, Copy, PartialEq, Eq)] -pub struct ReturnType(u32); - -impl From<u32> for ReturnType { - fn from(value: u32) -> ReturnType { - ReturnType(value) - } -} - -impl From<ReturnType> for u32 { - fn from(return_type: ReturnType) -> u32 { - return_type.0 - } -} - -pub const FAILURE: ReturnType = ReturnType(0); -pub const FAILURE_U32: ReturnType = ReturnType(1); -pub const FAILURE_2_U32: ReturnType = ReturnType(2); -pub const FAILURE_U64: ReturnType = ReturnType(3); -pub const SUCCESS: ReturnType = ReturnType(128); -pub const SUCCESS_U32: ReturnType = ReturnType(129); -pub const SUCCESS_2_U32: ReturnType = ReturnType(130); -pub const SUCCESS_U64: ReturnType = ReturnType(131); -pub const SUCCESS_3_U32: ReturnType = ReturnType(132); -pub const SUCCESS_U32_U64: ReturnType = ReturnType(133);
diff --git a/core/platform/src/return_variant.rs b/core/platform/src/return_variant.rs new file mode 100644 index 0000000..e0d6fbf --- /dev/null +++ b/core/platform/src/return_variant.rs
@@ -0,0 +1,32 @@ +/// `ReturnVariant` describes what value type the kernel has returned. +// ReturnVariant is not an enum so that it can be converted from a u32 for free. +// TODO: derive(Debug) is currently only enabled for test builds, which is +// necessary so it can be used in assert_eq!. We should develop a lighter-weight +// Debug implementation and see if it is small enough to enable on non-Debug +// builds. +#[cfg_attr(test, derive(Debug))] +#[derive(Clone, Copy, PartialEq, Eq)] +pub struct ReturnVariant(u32); + +impl From<u32> for ReturnVariant { + fn from(value: u32) -> ReturnVariant { + ReturnVariant(value) + } +} + +impl From<ReturnVariant> for u32 { + fn from(return_variant: ReturnVariant) -> u32 { + return_variant.0 + } +} + +pub const FAILURE: ReturnVariant = ReturnVariant(0); +pub const FAILURE_U32: ReturnVariant = ReturnVariant(1); +pub const FAILURE_2_U32: ReturnVariant = ReturnVariant(2); +pub const FAILURE_U64: ReturnVariant = ReturnVariant(3); +pub const SUCCESS: ReturnVariant = ReturnVariant(128); +pub const SUCCESS_U32: ReturnVariant = ReturnVariant(129); +pub const SUCCESS_2_U32: ReturnVariant = ReturnVariant(130); +pub const SUCCESS_U64: ReturnVariant = ReturnVariant(131); +pub const SUCCESS_3_U32: ReturnVariant = ReturnVariant(132); +pub const SUCCESS_U32_U64: ReturnVariant = ReturnVariant(133);
diff --git a/core/platform/src/syscalls_impl.rs b/core/platform/src/syscalls_impl.rs index 8f24467..506ad64 100644 --- a/core/platform/src/syscalls_impl.rs +++ b/core/platform/src/syscalls_impl.rs
@@ -1,6 +1,6 @@ //! Implements `Syscalls` for all types that implement `RawSyscalls`. -use crate::{return_type, RawSyscalls, Syscalls, YieldType}; +use crate::{return_variant, RawSyscalls, Syscalls, YieldType}; impl<S: RawSyscalls> Syscalls for S { // ------------------------------------------------------------------------- @@ -12,6 +12,6 @@ } fn yield_no_wait() -> bool { - Self::raw_yield(YieldType::NoWait) != return_type::FAILURE.into() + Self::raw_yield(YieldType::NoWait) != return_variant::FAILURE.into() } }