Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
repiv
Aug 13, 2009

Yeah, if you enable the "serde-serialize" feature in wasm-bindgen then you can pass anything that serde can serialize to JSON across the WASM boundary. For example you can do

code:
#[wasm_bindgen]
pub fn generate_hashmap() -> JsValue {
    let mut hashmap = HashMap::new();
    hashmap.insert(1, "one");
    hashmap.insert(2, "two");
    hashmap.insert(3, "three");

    JsValue::from_serde(&hashmap).unwrap()
}
and calling this function from JS will give you {1: "one", 2: "two", 3: "three"}. The reverse also works using JsValue::into_serde().

Adbot
ADBOT LOVES YOU

repiv
Aug 13, 2009

The wasm32-unknown-unknown target is available on stable Rust now, so there's no need for the setup process to involve installing nightly anymore :)

(unless you're using other nightly features)

repiv
Aug 13, 2009

Sounds like you want mem::swap

code:
use std::mem;

[...]

let mut output = HashMap::new();
mem::swap(&mut self.map, &mut output);
Ok(output)

repiv
Aug 13, 2009

The standard library has both - mem::swap/mem::replace do it safely through references, and ptr::swap/ptr::replace do it unsafely through raw pointers.

repiv
Aug 13, 2009

Measly Twerp posted:

For example, if you've ever tried to implement a tree data type in rust, you know it's not as easy as in a memory managed language, especially so if you need the children to know what their parent nodes are.

Arenas are another option provided you don't need to drop individual nodes. Nodes allocated from the same arena all have the same lifetime, so they can freely hold references to each other, circular references, whatever, the compiler doesn't care as long as the arena lives long enough.

e.g. https://docs.rs/typed-arena/1.4.1/typed_arena/#safe-cycles

repiv fucked around with this message at 15:39 on Jul 13, 2019

repiv
Aug 13, 2009

It might be worth reporting that, the Rust team generally treats unhelpful error messages as bugs.

repiv
Aug 13, 2009

Large arrays are just annoying to work with right now, the compiler will usually elide the temporary stack allocation in release builds but it's hard to keep your debug builds from blowing up. I think the "box" keyword is supposed to fix that eventually.

If you really want to use arrays and only care about storing zeroable POD types like f32 then the bytemuck crate has a helper that can construct a Box<[T; N]> without an intermediate stack allocation in the meantime

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=33d0630c79f834949a8d3d22e8c7c01e

That works for anything implementing bytemucks Zeroable trait, which can be derived on your own types too if all their fields are also Zeroable

repiv fucked around with this message at 22:53 on May 19, 2022

repiv
Aug 13, 2009

It feels like you should just be able to do Box::default() for that really, but Default is still only implemented for arrays up to 32 long

Not sure why there isn't a const-generic implementation of Default for any length array yet

repiv
Aug 13, 2009


seems like more drama has been brewing, one of the four members of the core team just resigned outright

https://twitter.com/jntrnr/status/1662229886386442242

Adbot
ADBOT LOVES YOU

repiv
Aug 13, 2009

on windows rust uses the MSVC linker so i guess there might be something similar on mac where it needs the native tools to put the executable together

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply