# Rust's Vec\<u8\> == OCaml's?

**URL:** <https://discuss.ocaml.org/t/rusts-vec-u8-ocamls/12479>\
**Category:** Learning\
**Created:** [June 26, 2023, 9:48am UTC](https://discuss.ocaml.org/t/rusts-vec-u8-ocamls/12479 "2023-06-26T09:48:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [June 26, 2023, 9:48am UTC](https://discuss.ocaml.org/t/rusts-vec-u8-ocamls/12479/1 "2023-06-26T09:48:48Z")

</div>

Context: I’m writing some serialization/deserialization routes for OCaml / Rust interop.

I am looking for a container over bytes where I can efficiently:

- read a u8/i8/u16/i16/u32/i32/f32
- write a u8/i8/u16/i16/u32/i32/f32
- everything is little endian
- also needs to work under jsoo

The closest I have found so far is: [Buffer (base.Base.Buffer)](https://ocaml.janestreet.com/ocaml-core/v0.12/doc/base/Base/Buffer/index.html)

but it’s lacking the read/write ops.

Suggestions?

---

<div class="post-metadata">

**Author:** ![EmileTrotignon](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/emiletrotignon/32/5913_2.png) [@EmileTrotignon](https://discuss.ocaml.org/u/EmileTrotignon)\
**Post date:** [June 26, 2023, 9:54am UTC](https://discuss.ocaml.org/t/rusts-vec-u8-ocamls/12479/2 "2023-06-26T09:54:07Z")

</div>

I think `Buffer` and `Bytes` provide this, but they will only allow to write char (that is u8). It is perfectly possible to write two of them to get to u16, but it might not be efficient enough.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [June 26, 2023, 9:58am UTC](https://discuss.ocaml.org/t/rusts-vec-u8-ocamls/12479/3 "2023-06-26T09:58:14Z")

</div>

I see, so basically I should expect to roll my own conversions for:

```auto
(u8, u8) <-> i16, u16
(u8, u8, u8, u8) -> i32, u32, f32

```

? (I’m surprised these are not builtins )

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [June 26, 2023, 9:59am UTC](https://discuss.ocaml.org/t/rusts-vec-u8-ocamls/12479/4 "2023-06-26T09:59:33Z")

</div>

The stdlib’s `Buffer` and `Bytes` do have efficient read write for sized ints since 4.08.

[This](https://v2.ocaml.org/api/) is where you want to look up the docs.

---

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/zeroexcuses/32/1692_2.png) [@zeroexcuses](https://discuss.ocaml.org/u/zeroexcuses)\
**Post date:** [June 26, 2023, 10:02am UTC](https://discuss.ocaml.org/t/rusts-vec-u8-ocamls/12479/5 "2023-06-26T10:02:56Z")

</div>

I see, are

```auto
val get_uint8 : bytes -> int -> int
get_uint8 b i is b's unsigned 8-bit integer starting at byte index i.

Since 4.08
val get_int8 : bytes -> int -> int
get_int8 b i is b's signed 8-bit integer starting at byte index i.

Since 4.08
val get_uint16_ne : bytes -> int -> int
get_uint16_ne b i is b's native-endian unsigned 16-bit integer starting at byte index i.

Since 4.08
val get_uint16_be : bytes -> int -> int

(and the corresponding set_*)

```

the functions you are referring to ?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [June 26, 2023, 10:21am UTC](https://discuss.ocaml.org/t/rusts-vec-u8-ocamls/12479/6 "2023-06-26T10:21:26Z")

</div>

Yes. Your other option is linear bigarrays of bytes (I don’t think you need a tutorial, the [API docs](https://v2.ocaml.org/api/Bigarray.html) seem sufficient to me, follow the types, it’s just arrays). But the integer primitives are not exposed on those.

You’ll find quite a few `Big{bytes,string}` modules lying around, [here’s one](https://github.com/dbuenzli/qoic/blob/ebbed73cb820a0a95768666448c8769a0b1ba181/src/qoic.ml#L25-L50) you could get started from. I think a `Bigbytes` module should be added in stdlib, but I’m waiting to see what happens with `bytes` in OCaml 5 before proposing one (if we can get non moveable `bytes` then a `Bigbytes` module would become redundant on 64-bit platforms).
