These are chat archives for rust-lang/rust

3rd
Mar 2016
Dmitry Golubets
@DGolubets
Mar 03 2016 01:35
@jnicholls Thanks for the example
Though it may be enough in real app, I'm still trying to do a solution without compromises in my playground app (os this simply helps me to understand all the tricky parts of the language). I've come with this monster so far (it seems to work, but there is some unsafe stuff): http://is.gd/gJip4O
Cameron Alexander
@emptyflash
Mar 03 2016 16:10
Anyone know around what time the release is supposed to happen today?
Erik Hedvall
@Ogeon
Mar 03 2016 16:16
Should be in a couple of hours, as far as I know, but I have no other info than a calendar.
Cameron Alexander
@emptyflash
Mar 03 2016 16:16
okay, cool beans
Cameron Alexander
@emptyflash
Mar 03 2016 17:27
Oh, cool. Like 30 more minutes according to that
Erik Hedvall
@Ogeon
Mar 03 2016 17:29
Yep :) I don't know if they follow it exactly, but it shouldn't be too long. Unless there are delays, of course.
mhsjlw
@mhsjlw
Mar 03 2016 20:31
I have a question about interfacing Rust inside of another language but I am problems. Should that question be asked here? Or should it be asked the programming language's gitter channel?
mhsjlw
@mhsjlw
Mar 03 2016 20:37
I am *experiencing problems
Erik Hedvall
@Ogeon
Mar 03 2016 20:39
I think you have a better chance of getting Rust related answers in Rust related channels/forums
mhsjlw
@mhsjlw
Mar 03 2016 20:41
Ok, well I have this program here: https://github.com/hansihe/voxel_worldgen which is a library for creating Minecraft-like generation. You execute the vanilla function and give the X and Y and it returns a struct with Vec3 and Vec inside.
I want to be able to compile the rust source into a lib then execute that function from inside Node.js
so that I can speedily generate the world then use it inside Node
However, when I try to use a function interface, I need to create the struct that the function returns. I am unable to figure out how the Vec datatype works, so I can't actually get the data.
Vladimir Matveev
@netvl
Mar 03 2016 20:43
@mhsjlw you cannot pass Rust collections across FFI boundaries
Peter Atashian
@retep998
Mar 03 2016 20:44
Well I mean you technically can but its unsafe and dangerous and rather unstable
Vladimir Matveev
@netvl
Mar 03 2016 20:44
you can only do that with structs marked as #[repr(C)] and primitive types
it is not just unstable, it is absolutely not supported. for example, Vec has a destructor, so its memory representation also contains a drop flag
mhsjlw
@mhsjlw
Mar 03 2016 20:45
That's what I notices when reading the Vec source code
GenUnit3<u8> is what it returns
Peter Atashian
@retep998
Mar 03 2016 20:45
The memory representation of any type that isn't #[repr(C)] is completely unspecified
And you cannot rely on it whatsoever
how can I just return a C datatype instead of a Vec
Vladimir Matveev
@netvl
Mar 03 2016 20:46
@mhsjlw therefore, you need to convert your data structures to some C-compatible form
mhsjlw
@mhsjlw
Mar 03 2016 20:46
Well, that is my question.
What do I change it to, and how.
Vladimir Matveev
@netvl
Mar 03 2016 20:46
you need to consider what structures you have and who should own them
if you only have simple POD types, consisting of primitives, then it makes sense to create a #[repr(C)] structure mirroring your Rust-side structure, copy the data from your Rust-side structure into it and just pass it across the FFI boundary by value
things get harder when you have dynamically sized collections like Vec, who also perform allocations
you need to determine who should own such pieces of data
Peter Atashian
@retep998
Mar 03 2016 20:49
And remember that the side which allocated such data is also responsible for freeing it
Because the allocator that one side uses is not necessarily the same one the other side uses
Vladimir Matveev
@netvl
Mar 03 2016 20:50
@mhsjlw also you need to find out the most appropriate representation of the data in your target language (JS, right?). I don't know JS FFI, but there should be some documentation on how C data types are represented on JS side
for example, there probably is a way to convert a pair (pointer to data, length of date) to a JS array

therefore, in order to pass a Vec to the C side you need to obtain a pointer to its data and its length, this can easily be done with Vec (actually slice) methods:

let data_ptr = vec.as_ptr();
let data_len = vec.len();

then you need to pass these values to the JS side and reconstruct its array from them

but unfortunately I know little of how JS allows one to handle ownership
Erik Hedvall
@Ogeon
Mar 03 2016 20:54
I think one way to do this, without converting the GenUint3<u8>, would be to box it, treat it as a void* (or some other kind of "black box"), create a C API around that and otherwise only communicate types that are C and JS friendly.
Vladimir Matveev
@netvl
Mar 03 2016 20:54
I don't know whether it could accept finalizers for custom data, for example
Erik Hedvall
@Ogeon
Mar 03 2016 20:54
It would be strange if there was no support for finalizing/destroying a value.
Vladimir Matveev
@netvl
Mar 03 2016 20:55
this does not actually solve the problem of converting data structures like vectors and arrays
Erik Hedvall
@Ogeon
Mar 03 2016 20:55
Is that a requirement, though?
Vladimir Matveev
@netvl
Mar 03 2016 20:56
I was under impression that yes, it is - @mhsjlw seems to want to get the actual numbers on the JS side
Erik Hedvall
@Ogeon
Mar 03 2016 20:57
I thought the main problem was to recreate the result struct, including the Vec
Vladimir Matveev
@netvl
Mar 03 2016 20:57
but of course, this depends on the situation - in many cases opaque data structure with accessors providing simple types is perfectly reasonable and correct thing to do
Erik Hedvall
@Ogeon
Mar 03 2016 21:00
Anyway, deconstructing and reconstructing a Vec requires the capacity, as well
Vladimir Matveev
@netvl
Mar 03 2016 21:01
you can always convert Vec<T> to Box<[T]>, which removes the need in capacity
mhsjlw
@mhsjlw
Mar 03 2016 21:56
I've just been looking over the FFI documentation that I'm using
Thank you for the help so far by the way
Maybe I'll figure out a way to not return Vecs, but instead just return C type values
that way, I don't have to make any crazy code changes