Home > Enterprise >  Rust equivalent of GCHandle.Alloc
Rust equivalent of GCHandle.Alloc

Time:10-17

What is the equivalent of this code in Rust?

byte[] bytes = [...]; // some given bytes
bytesInt = GCHandle.Alloc(bytes, GCHandleType.Pinned);
bytesIntPtr = bytesInt.AddrOfPinnedObject();

In Rust the resulting pointer should be of type *const c_void and the input is bytes: Vec<u8>.
I can't seem to figure it out.

CodePudding user response:

If you want a pointer to the vector's internal buffer, you can use as_ptr e.g.

let v: Vec<u8> = vec![1, 2, 3];
let p: *const u8 = v.as_ptr();
  • Related