A negative custom discriminant in a fieldless number enumeration seems unsupported
In Rust, it's OK to have a a negative discriminant for a number enum variant:
pub enum NumberEnum {
Foo = 0,
Bar = -1,
Qux = 2,
}
When compiled with wasm-bindgen on Mac OS, however, the following code:
#[wasm_bindgen]
pub enum NumberEnum {
Foo = 0,
Bar = -1,
Qux = 2,
}
Produces the following error
error: enums with #[wasm_bidngen] may only have number literal values
--> src/trade_manager.rs:704:11
|
704 | Bar = -1,
| ^^
Sound like wasm_bindgen with generate enum number literal value into u32:
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use wasm_bindgen::prelude::*;
pub enum NumberEnum {
Foo = 0,
Bar = 1,
Qux = 2,
}
#[allow(clippy::all)]
impl wasm_bindgen::convert::IntoWasmAbi for NumberEnum {
type Abi = u32;
#[inline]
fn into_abi(self) -> u32 {
self as u32
}
}
#[allow(clippy::all)]
impl wasm_bindgen::convert::FromWasmAbi for NumberEnum {
type Abi = u32;
#[inline]
unsafe fn from_abi(js: u32) -> Self {
if js == NumberEnum::Foo as u32 {
NumberEnum::Foo
} else if js == NumberEnum::Bar as u32 {
NumberEnum::Bar
} else if js == NumberEnum::Qux as u32 {
NumberEnum::Qux
} else {
wasm_bindgen::throw_str("invalid enum value passed")
}
}
}
#[allow(clippy::all)]
impl wasm_bindgen::convert::OptionFromWasmAbi for NumberEnum {
#[inline]
fn is_none(val: &u32) -> bool {
*val == 3u32
}
}
#[allow(clippy::all)]
impl wasm_bindgen::convert::OptionIntoWasmAbi for NumberEnum {
#[inline]
fn none() -> Self::Abi {
3u32
}
}
#[allow(clippy::all)]
impl wasm_bindgen::describe::WasmDescribe for NumberEnum {
fn describe() {
use wasm_bindgen::describe::*;
inform(ENUM);
inform(3u32);
}
}
And it looks like wasm_bindgen will not expect #[repr(i32)] here.
@Xuanwo Thanks, this looks correct
Could this be changed to support negative numbers?
Could this be changed to support negative numbers?
Hi, it's better to open a new issue to request this feature instead.
Could this be changed to support negative numbers?
From the top of my head I don't see why not. But somebody would have to take a closer look.
In any case I'm happy to review a PR.