Skip to content

Commit 78a389b

Browse files
authored
Merge pull request #22 from Neotron-Compute/api-fixes
API fixes and Option/Result conversions
2 parents b488196 + eea0e2f commit 78a389b

File tree

2 files changed

+50
-22
lines changed

2 files changed

+50
-22
lines changed

src/lib.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,17 @@ pub struct Api {
8989
/// that is an Operating System level design feature. These APIs just
9090
/// reflect the raw hardware, in a similar manner to the registers exposed
9191
/// by a memory-mapped UART peripheral.
92-
pub serial_get_info: extern "C" fn(device: u8) -> crate::Option<serial::DeviceInfo>,
92+
pub serial_get_info: extern "C" fn(device_id: u8) -> crate::Option<serial::DeviceInfo>,
9393
/// Set the options for a given serial device. An error is returned if the
9494
/// options are invalid for that serial device.
95-
pub serial_configure: extern "C" fn(device: u8, config: serial::Config) -> crate::Result<()>,
95+
pub serial_configure: extern "C" fn(device_id: u8, config: serial::Config) -> crate::Result<()>,
9696
/// Write bytes to a serial port. There is no sense of 'opening' or
9797
/// 'closing' the device - serial devices are always open. If the return
9898
/// value is `Ok(n)`, the value `n` may be less than the size of the given
9999
/// buffer. If so, that means not all of the data could be transmitted -
100100
/// only the first `n` bytes were.
101101
pub serial_write: extern "C" fn(
102-
device: u8,
102+
device_id: u8,
103103
data: ApiByteSlice,
104104
timeout: crate::Option<Timeout>,
105105
) -> crate::Result<usize>,
@@ -110,7 +110,7 @@ pub struct Api {
110110
/// could be received - only the first `n` bytes were (and hence only the
111111
/// first `n` bytes of the given buffer now contain data).
112112
pub serial_read: extern "C" fn(
113-
device: u8,
113+
device_id: u8,
114114
data: ApiBuffer,
115115
timeout: crate::Option<Timeout>,
116116
) -> crate::Result<usize>,
@@ -320,7 +320,7 @@ pub struct Api {
320320
///
321321
/// I²C Bus 0 should be the one connected to the Neotron Bus.
322322
/// I²C Bus 1 is typically the VGA DDC bus.
323-
pub i2c_bus_get_info: extern "C" fn(i2c_bus: u8) -> crate::Option<i2c::BusInfo>,
323+
pub i2c_bus_get_info: extern "C" fn(bus_id: u8) -> crate::Option<i2c::BusInfo>,
324324
/// Transact with a I²C Device on an I²C Bus
325325
///
326326
/// * `i2c_bus` - Which I²C Bus to use
@@ -341,7 +341,7 @@ pub struct Api {
341341
/// # Ok::<(), neotron_common_bios::Error>(())
342342
/// ```
343343
pub i2c_write_read: extern "C" fn(
344-
i2c_bus: u8,
344+
bus_id: u8,
345345
i2c_device_address: u8,
346346
tx: ApiByteSlice,
347347
tx2: ApiByteSlice,
@@ -353,7 +353,7 @@ pub struct Api {
353353
// ========================================================================
354354
/// Get information about the Audio Mixer channels
355355
pub audio_mixer_channel_get_info:
356-
extern "C" fn(audio_mixer_id: u8) -> crate::Result<audio::MixerChannelInfo>,
356+
extern "C" fn(audio_mixer_id: u8) -> crate::Option<audio::MixerChannelInfo>,
357357
/// Set an Audio Mixer level
358358
pub audio_mixer_channel_set_level:
359359
extern "C" fn(audio_mixer_id: u8, level: u8) -> crate::Result<()>,
@@ -530,12 +530,12 @@ pub struct Api {
530530
/// The set of devices is not expected to change at run-time - removal of
531531
/// media is indicated with a boolean field in the
532532
/// `block_dev::DeviceInfo` structure.
533-
pub block_dev_get_info: extern "C" fn(device: u8) -> crate::Option<block_dev::DeviceInfo>,
533+
pub block_dev_get_info: extern "C" fn(device_id: u8) -> crate::Option<block_dev::DeviceInfo>,
534534
/// Eject a disk from the drive.
535535
///
536536
/// Will return an error if this device is not removable. Does not return an
537537
/// error if the drive is already empty.
538-
pub block_dev_eject: extern "C" fn(device: u8) -> crate::Result<()>,
538+
pub block_dev_eject: extern "C" fn(device_id: u8) -> crate::Result<()>,
539539
/// Write one or more sectors to a block device.
540540
///
541541
/// The function will block until all data is written. The array pointed
@@ -545,7 +545,7 @@ pub struct Api {
545545
/// There are no requirements on the alignment of `data` but if it is
546546
/// aligned, the BIOS may be able to use a higher-performance code path.
547547
pub block_write: extern "C" fn(
548-
device: u8,
548+
device_id: u8,
549549
start_block: block_dev::BlockIdx,
550550
num_blocks: u8,
551551
data: ApiByteSlice,
@@ -559,7 +559,7 @@ pub struct Api {
559559
/// There are no requirements on the alignment of `data` but if it is
560560
/// aligned, the BIOS may be able to use a higher-performance code path.
561561
pub block_read: extern "C" fn(
562-
device: u8,
562+
device_id: u8,
563563
start_block: block_dev::BlockIdx,
564564
num_blocks: u8,
565565
data: ApiBuffer,
@@ -574,7 +574,7 @@ pub struct Api {
574574
/// There are no requirements on the alignment of `data` but if it is
575575
/// aligned, the BIOS may be able to use a higher-performance code path.
576576
pub block_verify: extern "C" fn(
577-
device: u8,
577+
device_id: u8,
578578
start_block: block_dev::BlockIdx,
579579
num_blocks: u8,
580580
data: ApiByteSlice,

src/types.rs

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,23 +170,51 @@ pub struct MemoryRegion {
170170
impl<T> Result<T> {
171171
/// Obtain the inner value, or panic - just like `core::Result::unwrap`.
172172
pub fn unwrap(self) -> T {
173-
match self {
174-
crate::Result::Ok(val) => val,
175-
crate::Result::Err(e) => {
176-
panic!("Unwrap called, got err {:?}", e);
177-
}
173+
let r: core::result::Result<T, Error> = self.into();
174+
r.unwrap()
175+
}
176+
}
177+
178+
impl<T> From<core::result::Result<T, Error>> for Result<T> {
179+
fn from(value: core::result::Result<T, Error>) -> Self {
180+
match value {
181+
Ok(inner) => Result::Ok(inner),
182+
Err(inner) => Result::Err(inner),
183+
}
184+
}
185+
}
186+
187+
impl<T> From<Result<T>> for core::result::Result<T, Error> {
188+
fn from(value: Result<T>) -> Self {
189+
match value {
190+
Result::Ok(inner) => core::result::Result::Ok(inner),
191+
Result::Err(inner) => core::result::Result::Err(inner),
178192
}
179193
}
180194
}
181195

182196
impl<T> Option<T> {
183197
/// Obtain the inner value, or panic - just like `core::Option::unwrap`.
184198
pub fn unwrap(self) -> T {
185-
match self {
186-
crate::Option::Some(val) => val,
187-
crate::Option::None => {
188-
panic!("Unwrap called on empty option");
189-
}
199+
let o: core::option::Option<T> = self.into();
200+
o.unwrap()
201+
}
202+
}
203+
204+
impl<T> From<core::option::Option<T>> for Option<T> {
205+
fn from(value: core::option::Option<T>) -> Self {
206+
match value {
207+
Some(inner) => Option::Some(inner),
208+
None => Option::None,
209+
}
210+
}
211+
}
212+
213+
impl<T> From<Option<T>> for core::option::Option<T> {
214+
fn from(value: Option<T>) -> Self {
215+
match value {
216+
Option::Some(inner) => core::option::Option::Some(inner),
217+
Option::None => core::option::Option::None,
190218
}
191219
}
192220
}

0 commit comments

Comments
 (0)