You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.
I am trying to port vinoski/bitwise NIF examples to Rust, but I found that Rust compiler does not let me update the contents of the data field of ErlNifBinary.
I am not totally sure as I just started to play with Rust a few days ago, but it looks like the data field should be defined as *mut u8 instead of *const u8?
/// Erlang: -spec exor(binary(), byte::0..255) -> binary().////// exor misbehaves on a regular scheduler thread when the incomng binary is/// large because it blocks the thread for too long. But it works fine on a/// dirty scheduler.extern"C"fnexor(env:*mutErlNifEnv,argc:c_int,args:*constERL_NIF_TERM) -> ERL_NIF_TERM{unsafe{letmut bin:ErlNifBinary = uninitialized();letmut outbin:ErlNifBinary = uninitialized();letmut val:c_uint = uninitialized();if argc != 2
|| 0 == enif_inspect_binary(env,*args,&mut bin)
|| 0 == enif_get_uint(env,*args.offset(1),&mut val)
|| val > 255{returnenif_make_badarg(env);}if bin.size == 0{return*args;}enif_alloc_binary(bin.size,&mut outbin);do_xor(bin.data, bin.size, outbin.data, val asu8);// <-- This line does not compile.// @TODO: Implement enif_make_tuple2() and friends in ruster_unsafe// enif_make_tuple2(env,// enif_make_binary(env, &mut outbin),// enif_make_int(env, 0))enif_make_binary(env,&mut outbin)}}fndo_xor(bin_data:*constu8,length:usize,outbin_data:*mutu8,byte:u8){let bin_slice:&[u8] = unsafe{slice::from_raw_parts(bin_data, length)};let outbin_slice:&mut[u8] = unsafe{slice::from_raw_parts_mut(outbin_data, length)};for(b, ob)in bin_slice.iter().zip(outbin_slice.iter_mut()){(*ob) = (*b) ^ byte;}}
And the compile error:
src/lib.rs:75:36: 75:47 error: mismatched types: expected `*mut u8`, found `*const u8`(values differ in mutability) [E0308]src/lib.rs:75 do_xor(bin.data, bin.size, outbin.data, val as u8); ^~~~~~~~~~~src/lib.rs:75:36: 75:47 help: run `rustc --explain E0308` to see a detailed explanationerror: aborting due to previous errorCould not compile `bitwise_rust_erlang`.
If I change the data field to *mut u8, it will compile.
I am trying to port vinoski/bitwise NIF examples to Rust, but I found that Rust compiler does not let me update the contents of the
data
field ofErlNifBinary
.I am not totally sure as I just started to play with Rust a few days ago, but it looks like the
data
field should be defined as*mut u8
instead of*const u8
?ruster_unsafe/src/lib.rs
Here is relevant code from my program. (Ported from bitwise_nif.c#L14-L38)
And the compile error:
If I change the
data
field to*mut u8
, it will compile.The text was updated successfully, but these errors were encountered: