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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
use std::borrow::Cow;
use crate::wasm::{WasmType, WasmTypeKind, WasmValueError};
/// The WasmValue trait may be implemented to represent values to be
/// (de)serialized with WAVE, notably [`value::Value`](crate::value::Value)
/// and [`wasmtime::component::Val`].
///
/// The `make_*` and `unwrap_*` methods should be called only for corresponding
/// [`WasmTypeKind`](crate::wasm::WasmTypeKind)s.
#[allow(unused_variables)]
pub trait WasmValue: Clone + Sized {
/// A type representing types of these values.
type Type: WasmType;
/// The kind of type of this value.
fn kind(&self) -> WasmTypeKind;
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_bool(val: bool) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_s8(val: i8) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_s16(val: i16) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_s32(val: i32) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_s64(val: i64) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_u8(val: u8) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_u16(val: u16) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_u32(val: u32) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_u64(val: u64) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
///
/// The Rust `f32` type has many distinct NaN bitpatterns, however the
/// component-model `float32` type only has a single NaN value, so this
/// function does not preserve NaN bitpatterns.
///
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_float32(val: f32) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
///
/// The Rust `f64` type has many distinct NaN bitpatterns, however the
/// component-model `float64` type only has a single NaN value, so this
/// function does not preserve NaN bitpatterns.
///
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_float64(val: f64) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_char(val: char) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_string(val: Cow<str>) -> Self {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_list(
ty: &Self::Type,
vals: impl IntoIterator<Item = Self>,
) -> Result<Self, WasmValueError> {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
///
/// The fields provided by `fields` are not necessarily sorted; the callee
/// should perform sorting itself if needed.
///
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_record<'a>(
ty: &Self::Type,
fields: impl IntoIterator<Item = (&'a str, Self)>,
) -> Result<Self, WasmValueError> {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_tuple(
ty: &Self::Type,
vals: impl IntoIterator<Item = Self>,
) -> Result<Self, WasmValueError> {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_variant(
ty: &Self::Type,
case: &str,
val: Option<Self>,
) -> Result<Self, WasmValueError> {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_enum(ty: &Self::Type, case: &str) -> Result<Self, WasmValueError> {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_option(ty: &Self::Type, val: Option<Self>) -> Result<Self, WasmValueError> {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_result(
ty: &Self::Type,
val: Result<Option<Self>, Option<Self>>,
) -> Result<Self, WasmValueError> {
unimplemented!()
}
/// Returns a new WasmValue of the given type.
///
/// The strings provided by `names` are not necessarily sorted; the callee
/// should perform sorting itself if needed.
///
/// # Panics
/// Panics if the type is not implemented (the trait default).
fn make_flags<'a>(
ty: &Self::Type,
names: impl IntoIterator<Item = &'a str>,
) -> Result<Self, WasmValueError> {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_bool(&self) -> bool {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_s8(&self) -> i8 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_s16(&self) -> i16 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_s32(&self) -> i32 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_s64(&self) -> i64 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_u8(&self) -> u8 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_u16(&self) -> u16 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_u32(&self) -> u32 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_u64(&self) -> u64 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
///
/// The Rust `f32` type has many distinct NaN bitpatterns, however the
/// component-model `float64` type only has a single NaN value, so this
/// function does not preserve NaN bitpatterns.
///
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_float32(&self) -> f32 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
///
/// The Rust `f64` type has many distinct NaN bitpatterns, however the
/// component-model `float64` type only has a single NaN value, so this
/// function does not preserve NaN bitpatterns.
///
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_float64(&self) -> f64 {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_char(&self) -> char {
unimplemented!()
}
/// Returns the underlying value of the WasmValue, panicing if it's the wrong type.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_string(&self) -> Cow<str> {
unimplemented!()
}
/// Returns an iterator of the element Vals of the list.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_list(&self) -> Box<dyn Iterator<Item = Cow<Self>> + '_> {
unimplemented!()
}
/// Returns an iterator of the field names and Vals of the record.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_record(&self) -> Box<dyn Iterator<Item = (Cow<str>, Cow<Self>)> + '_> {
unimplemented!()
}
/// Returns an iterator of the field Vals of the tuple.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_tuple(&self) -> Box<dyn Iterator<Item = Cow<Self>> + '_> {
unimplemented!()
}
/// Returns the variant case name and optional payload WasmValue of the variant.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_variant(&self) -> (Cow<str>, Option<Cow<Self>>) {
unimplemented!()
}
/// Returns the case name of the enum.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_enum(&self) -> Cow<str> {
unimplemented!()
}
/// Returns the optional WasmValue.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_option(&self) -> Option<Cow<Self>> {
unimplemented!()
}
/// Returns Ok(_) or Err(_) with the optional payload WasmValue.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_result(&self) -> Result<Option<Cow<Self>>, Option<Cow<Self>>> {
unimplemented!()
}
/// Returns an iterator of the names of the flags WasmValue.
/// # Panics
/// Panics if `self` is not of the right type.
fn unwrap_flags(&self) -> Box<dyn Iterator<Item = Cow<str>> + '_> {
unimplemented!()
}
}
macro_rules! unwrap_val {
($val:expr, $case:path, $name:expr) => {
match $val {
$case(v) => v,
_ => panic!("called unwrap_{name} on non-{name} value", name = $name),
}
};
}
macro_rules! unwrap_2val {
($val:expr, $case:path, $name:expr) => {
match $val {
$case(n, v) => (n, v),
_ => panic!("called unwrap_{name} on non-{name} value", name = $name),
}
};
}
pub(crate) use unwrap_2val;
pub(crate) use unwrap_val;