Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert Vec<[u8]> to JsValue #1134

Closed
lcnr opened this issue Dec 28, 2018 · 5 comments
Closed

convert Vec<[u8]> to JsValue #1134

lcnr opened this issue Dec 28, 2018 · 5 comments

Comments

@lcnr
Copy link
Contributor

lcnr commented Dec 28, 2018

I am using fetch in combination with bincode. As bincode::deserialize expects a byte array and bincode::serialize creates a byte array. To do this I currently use the following code:

let data = bincode::serialize(content).unwrap()
opts.body(Some(&JsValue::from_str(unsafe { std::str::from_utf8_unchecked(data) })));

Is there a better way to do this?

@alexcrichton
Copy link
Contributor

Currently there's not a great way to do this inline in Rust, but it's covered by #811 so I'm going to close this in favor of that. Thanks for the report though!

@rylev
Copy link
Contributor

rylev commented Jan 19, 2019

@alexcrichton I have a Vec<f64> and I need a &JsValue. I believe it's not possible to do this conversion without doing the serialization trick. Am I missing something?

@alexcrichton
Copy link
Contributor

@rylev I think Float64Array::view might do the trick for you?

@rylev
Copy link
Contributor

rylev commented Jan 19, 2019

I'm looking for a safe variant. I end up building a new Float64Array, coping over the floats into the new array, and then calling as_ref to get a JsValue. I wonder if it would be a good idea to have an API for doing just that (i.e., copying over a collection from the wams heap to the JS heap. Could be for any collection of T: Clone.

@alexcrichton
Copy link
Contributor

I believe if you slice() just afterwards you can make a safe wrapper as that'll do a copy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants