use crate::{Pages, ValueType};
use core::ops::SubAssign;
use rkyv::{Archive, Deserialize as RkyvDeserialize, Serialize as RkyvSerialize};
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
use std::convert::{TryFrom, TryInto};
use std::iter::Sum;
use std::ops::{Add, AddAssign};
#[derive(
Debug,
Clone,
Copy,
PartialEq,
Eq,
Hash,
RkyvSerialize,
RkyvDeserialize,
Archive,
rkyv::CheckBytes,
)]
#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "artifact-size", derive(loupe::MemoryUsage))]
#[archive(as = "Self")]
#[repr(u8)]
pub enum MemoryStyle {
Dynamic {
offset_guard_size: u64,
},
Static {
bound: Pages,
offset_guard_size: u64,
},
}
impl MemoryStyle {
pub fn offset_guard_size(&self) -> u64 {
match self {
Self::Dynamic { offset_guard_size } => *offset_guard_size,
Self::Static {
offset_guard_size, ..
} => *offset_guard_size,
}
}
}
pub unsafe trait MemorySize: Copy {
type Offset: Default
+ std::fmt::Debug
+ std::fmt::Display
+ Eq
+ Ord
+ PartialEq<Self::Offset>
+ PartialOrd<Self::Offset>
+ Clone
+ Copy
+ Sync
+ Send
+ ValueType
+ Into<u64>
+ From<u32>
+ From<u16>
+ From<u8>
+ TryFrom<u64>
+ TryFrom<u32>
+ TryFrom<u16>
+ TryFrom<u8>
+ TryFrom<i32>
+ TryInto<usize>
+ TryInto<u64>
+ TryInto<u32>
+ TryInto<u16>
+ TryInto<u8>
+ TryInto<i32>
+ TryFrom<usize>
+ Add<Self::Offset>
+ Sum<Self::Offset>
+ AddAssign<Self::Offset>
+ SubAssign<Self::Offset>
+ 'static;
type Native: super::NativeWasmType;
const ZERO: Self::Offset;
const ONE: Self::Offset;
fn offset_to_native(offset: Self::Offset) -> Self::Native;
fn native_to_offset(native: Self::Native) -> Self::Offset;
fn is_64bit() -> bool;
}
#[derive(Clone, Copy)]
pub struct Memory32;
unsafe impl MemorySize for Memory32 {
type Offset = u32;
type Native = i32;
const ZERO: Self::Offset = 0;
const ONE: Self::Offset = 1;
fn offset_to_native(offset: Self::Offset) -> Self::Native {
offset as Self::Native
}
fn native_to_offset(native: Self::Native) -> Self::Offset {
native as Self::Offset
}
fn is_64bit() -> bool {
false
}
}
#[derive(Clone, Copy)]
pub struct Memory64;
unsafe impl MemorySize for Memory64 {
type Offset = u64;
type Native = i64;
const ZERO: Self::Offset = 0;
const ONE: Self::Offset = 1;
fn offset_to_native(offset: Self::Offset) -> Self::Native {
offset as Self::Native
}
fn native_to_offset(native: Self::Native) -> Self::Offset {
native as Self::Offset
}
fn is_64bit() -> bool {
true
}
}