diff options
author | 2022-09-09 19:45:20 -0700 | |
---|---|---|
committer | 2022-09-09 19:45:20 -0700 | |
commit | d114e9040ba5815c120dc053e29da48803414c8a (patch) | |
tree | 32ff30c575f8768130b6a4d5637e5ab32695c378 | |
parent | c600196b1a566b575d32bbdc4229f78c7dc3eb81 (diff) | |
download | bun-d114e9040ba5815c120dc053e29da48803414c8a.tar.gz bun-d114e9040ba5815c120dc053e29da48803414c8a.tar.zst bun-d114e9040ba5815c120dc053e29da48803414c8a.zip |
Add `read` to README
-rw-r--r-- | README.md | 41 |
1 files changed, 39 insertions, 2 deletions
@@ -2823,11 +2823,13 @@ rustc --crate-type cdylib add.rs | i8 | `int8_t` | `int8_t` | | i16 | `int16_t` | `int16_t` | | i32 | `int32_t` | `int32_t`, `int` | -| i64 | `int64_t` | `int32_t` | +| i64 | `int64_t` | `int64_t` | +| i64_fast | `int64_t` | | | u8 | `uint8_t` | `uint8_t` | | u16 | `uint16_t` | `uint16_t` | | u32 | `uint32_t` | `uint32_t` | -| u64 | `uint64_t` | `uint32_t` | +| u64 | `uint64_t` | `uint64_t` | +| u64_fast | `uint64_t` | | | f32 | `float` | `float` | | f64 | `double` | `double` | | bool | `bool` | | @@ -2998,6 +3000,41 @@ const myPtr = ptr(myTypedArray); myTypedArray = new Uint8Array(toArrayBuffer(myPtr, 0, 32), 0, 32); ``` +**To read data from a pointer**: + +You have two options. + +For long-lived pointers, a `DataView` is the fastest option: + +```ts +import { toArrayBuffer } from "bun:ffi"; +var myDataView = new DataView(toArrayBuffer(myPtr, 0, 32)); + +console.log( + myDataView.getUint8(0, true), + myDataView.getUint8(1, true), + myDataView.getUint8(2, true), + myDataView.getUint8(3, true) +); +``` + +_Available in Bun v0.1.12+_ + +For short-lived pointers, `read` is the fastest option: + +```ts +import { read } from "bun:ffi"; + +console.log( + read.u8(myPtr, 0), + read.u8(myPtr, 1), + read.u8(myPtr, 2), + read.u8(myPtr, 3) +); +``` + +Read behaves similarly to `DataView`, but it can be faster because it doesn't need to create a `DataView` or `ArrayBuffer`. + **Memory management with pointers**: `bun:ffi` does not manage memory for you because it doesn't have the information necessary. You must free the memory when you're done with it. |