wasm-bindgen
wasm-bindgen copied to clipboard
#[wasm_bindgen] on Boxed structs
Summary
I'm attempting to generate JS for a simple struct in Rust -
#[wasm_bindgen]
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
#[wasm_bindgen]
impl ListNode {
#[wasm_bindgen(constructor)]
#[inline]
pub fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
I get two errors in this code, the first on the first line #[wasm_bindgen]
the trait bound `std::boxed::Box<ListNode>: wasm_bindgen::convert::OptionIntoWasmAbi` is not satisfied
the trait `wasm_bindgen::convert::OptionIntoWasmAbi` is not implemented for `std::boxed::Box<ListNode>`
help: the following implementations were found:
<std::boxed::Box<[T]> as wasm_bindgen::convert::OptionIntoWasmAbi>
<std::boxed::Box<[f32]> as wasm_bindgen::convert::OptionIntoWasmAbi>
<std::boxed::Box<[f64]> as wasm_bindgen::convert::OptionIntoWasmAbi>
<std::boxed::Box<[i16]> as wasm_bindgen::convert::OptionIntoWasmAbi>
and 10 others
note: required because of the requirements on the impl of `wasm_bindgen::convert::IntoWasmAbi` for `std::option::Option<std::boxed::Box<ListNode>>`rustc(E0277)
lib.rs(41, 1): the trait `wasm_bindgen::convert::OptionIntoWasmAbi` is not implemented for `std::boxed::Box<ListNode>`
and the second on the 5th line pub next: Option<Box<ListNode>>
the trait bound `std::boxed::Box<ListNode>: std::marker::Copy` is not satisfied
the trait `std::marker::Copy` is not implemented for `std::boxed::Box<ListNode>`
note: required because of the requirements on the impl of `std::marker::Copy` for `std::option::Option<std::boxed::Box<ListNode>>`rustc(E0277)
lib.rs(41, 1): required by this bound in `__wbg_get_listnode_next__const::__wbg_get_listnode_next::assert_copy`
lib.rs(45, 15): the trait `std::marker::Copy` is not implemented for `std::boxed::Box<ListNode>`
I tried to impl Copy for Box<ListNode>
but since it's a Box it gives me
the trait `Copy` may not be implemented for this type; the type has a destructor
Copy not allowed on types with destructorsrustc(E0184)
lib.rs(57, 1): Copy not allowed on types with destructors
How should I approach doing this?
Thanks!
Additional Details
Provide any additional details here.
Although im not sure what youre attempting to do but the only thing i could think of from the top of my head that may reasonable pub from that field and maybe have a function to obtain the next value and mapping it to a new struct?
#[wasm_bindgen]
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
next: Option<Box<ListNode>>,
}
#[wasm_bindgen]
impl ListNode {
#[wasm_bindgen(constructor)]
#[inline]
pub fn new(val: i32) -> Self {
ListNode { next: None, val }
}
pub fn next(&self) -> Option<ListNode> {
self.next.as_ref().map(|n| ListNode {
val: n.val, next: None
})
}
}
Not exactly a preferable option though