|
1 |
| -use crate::Pages; |
| 1 | +use crate::{Pages, ValueType}; |
2 | 2 | #[cfg(feature = "enable-rkyv")]
|
3 | 3 | use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
|
4 | 4 | #[cfg(feature = "enable-serde")]
|
5 | 5 | use serde::{Deserialize, Serialize};
|
| 6 | +use std::convert::{TryFrom, TryInto}; |
| 7 | +use std::iter::Sum; |
| 8 | +use std::ops::{Add, AddAssign}; |
6 | 9 |
|
7 | 10 | /// Implementation styles for WebAssembly linear memory.
|
8 | 11 | #[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
@@ -43,3 +46,79 @@ impl MemoryStyle {
|
43 | 46 | }
|
44 | 47 | }
|
45 | 48 | }
|
| 49 | + |
| 50 | +/// Trait for the `Memory32` and `Memory64` marker types. |
| 51 | +/// |
| 52 | +/// This allows code to be generic over 32-bit and 64-bit memories. |
| 53 | +pub unsafe trait MemorySize: Copy { |
| 54 | + /// Type used to represent an offset into a memory. This is `u32` or `u64`. |
| 55 | + type Offset: Default + |
| 56 | + std::fmt::Debug + |
| 57 | + std::fmt::Display + |
| 58 | + Eq + |
| 59 | + Ord + |
| 60 | + PartialEq<Self::Offset> + |
| 61 | + PartialOrd<Self::Offset> + |
| 62 | + Clone + |
| 63 | + Copy + |
| 64 | + ValueType + |
| 65 | + Into<u64> + |
| 66 | + From<u32> + |
| 67 | + From<u16> + |
| 68 | + From<u8> + |
| 69 | + TryFrom<u64> + |
| 70 | + TryFrom<u32> + |
| 71 | + TryFrom<u16> + |
| 72 | + TryFrom<u8> + |
| 73 | + TryInto<usize> + |
| 74 | + TryInto<u64> + |
| 75 | + TryInto<u32> + |
| 76 | + TryInto<u16> + |
| 77 | + TryInto<u8> + |
| 78 | + TryFrom<usize> + |
| 79 | + Add<Self::Offset> + |
| 80 | + Sum<Self::Offset> + |
| 81 | + AddAssign<Self::Offset>; |
| 82 | + |
| 83 | + /// Type used to pass this value as an argument or return value for a Wasm function. |
| 84 | + type Native: super::NativeWasmType; |
| 85 | + |
| 86 | + /// Zero value used for `WasmPtr::is_null`. |
| 87 | + const ZERO: Self::Offset; |
| 88 | + |
| 89 | + /// Convert an `Offset` to a `Native`. |
| 90 | + fn offset_to_native(offset: Self::Offset) -> Self::Native; |
| 91 | + |
| 92 | + /// Convert a `Native` to an `Offset`. |
| 93 | + fn native_to_offset(native: Self::Native) -> Self::Offset; |
| 94 | +} |
| 95 | + |
| 96 | +/// Marker trait for 32-bit memories. |
| 97 | +#[derive(Clone, Copy)] |
| 98 | +pub struct Memory32; |
| 99 | +unsafe impl MemorySize for Memory32 { |
| 100 | + type Offset = u32; |
| 101 | + type Native = i32; |
| 102 | + const ZERO: Self::Offset = 0; |
| 103 | + fn offset_to_native(offset: Self::Offset) -> Self::Native { |
| 104 | + offset as Self::Native |
| 105 | + } |
| 106 | + fn native_to_offset(native: Self::Native) -> Self::Offset { |
| 107 | + native as Self::Offset |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/// Marker trait for 64-bit memories. |
| 112 | +#[derive(Clone, Copy)] |
| 113 | +pub struct Memory64; |
| 114 | +unsafe impl MemorySize for Memory64 { |
| 115 | + type Offset = u64; |
| 116 | + type Native = i64; |
| 117 | + const ZERO: Self::Offset = 0; |
| 118 | + fn offset_to_native(offset: Self::Offset) -> Self::Native { |
| 119 | + offset as Self::Native |
| 120 | + } |
| 121 | + fn native_to_offset(native: Self::Native) -> Self::Offset { |
| 122 | + native as Self::Offset |
| 123 | + } |
| 124 | +} |
0 commit comments