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
//! Fixed-width slot conversion traits. They do not select an output memory
//! destination or provide an error return.
/// Decode `Self` from exactly `N` slots.
///
/// [`abi!`](crate::abi) infers `Self` from the business parameter type and
/// `N` from its bracketed slot group.
///
/// # Example
///
/// ```
/// use abi_adapter::FromSlots;
///
/// #[derive(Debug, PartialEq)]
/// struct Point { x: u32, y: u32 }
///
/// impl FromSlots<2> for Point {
/// fn from_slots([x, y]: [u32; 2]) -> Self {
/// Self { x, y }
/// }
/// }
///
/// assert_eq!(Point::from_slots([3, 5]), Point { x: 3, y: 5 });
/// ```
///
/// # Contract
///
/// Implementations must remain memory-safe for every slot bit pattern.
/// There is no error return; implementations may panic. The adapter does not
/// catch panics, and a panic in a guest `extern "C"` wrapper terminates the
/// program. See [`abi!`](crate::abi) for host and guest panic behavior.
///
/// Fallible validation can stay in the business function, with an agreed
/// status value as its result.
/// Encode `Self` as exactly `N` slots.
///
/// # Example
///
/// ```
/// use abi_adapter::IntoSlots;
///
/// struct Status(u32);
///
/// impl IntoSlots<1> for Status {
/// fn into_slots(self) -> [u32; 1] {
/// [self.0]
/// }
/// }
///
/// assert_eq!(Status(7).into_slots(), [7]);
/// ```
///
/// # Return width
///
/// This trait supports arbitrary `N`, but [`abi!`](crate::abi) directly
/// returns only zero or one slot. A `-> 0` declaration still invokes
/// `IntoSlots<0>`, discards the empty array, and returns `()`.
/// Larger results require a separate memory or resource protocol.
///
/// Panic behavior follows the contract of [`FromSlots`].