-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.rs
86 lines (72 loc) · 2.66 KB
/
option.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! FFI-safe types used in the various Neotron APIs.
//!
//! Note that all types in this file that are exported in the `Api` structure
//! *must* be `#[repr(C)]` and ABI stable.
// ============================================================================
// Imports
// ============================================================================
// None
// ============================================================================
// Constants
// ============================================================================
// None
// ============================================================================
// Types
// ============================================================================
/// All API functions which take/return optional values return this type.
///
/// We don't use the `Option` type from the standard library because that isn't
/// FFI safe and may change layout between compiler versions.
#[repr(C)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FfiOption<T> {
/// There is some data (the same as `core::option::Option::Some`)
Some(T),
/// There is no data (the same as `core::option::Option::None`)
None,
}
impl<T> FfiOption<T> {
/// Obtain the inner value, or panic - just like `core::Option::unwrap`.
pub fn unwrap(self) -> T {
let o: core::option::Option<T> = self.into();
o.unwrap()
}
}
impl<T> From<core::option::Option<T>> for crate::FfiOption<T> {
fn from(value: core::option::Option<T>) -> Self {
match value {
core::option::Option::Some(x) => crate::FfiOption::Some(x),
core::option::Option::None => crate::FfiOption::None,
}
}
}
impl<T> From<crate::FfiOption<T>> for core::option::Option<T> {
fn from(value: crate::FfiOption<T>) -> core::option::Option<T> {
match value {
crate::FfiOption::Some(x) => core::option::Option::Some(x),
crate::FfiOption::None => core::option::Option::None,
}
}
}
// ============================================================================
// Tests
// ============================================================================
#[cfg(test)]
mod test {
use super::*;
#[test]
fn make_option() {
let native: core::option::Option<i32> = Some(1234);
let ffi: FfiOption<i32> = native.into();
println!("ffi = {:?}", ffi);
}
#[test]
#[should_panic]
fn option_unwrap() {
let value: FfiOption<i32> = FfiOption::None;
let _x = value.unwrap();
}
}
// ============================================================================
// End of File
// ============================================================================