package core

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file core_bin_prot.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
open! Import
include Bin_prot

module Writer = struct
  type 'a t = 'a Bin_prot.Type_class.writer =
    { size : 'a Size.sizer
    ; write : 'a Write.writer
    }

  let to_string t v =
    let len = t.size v in
    let buf = Bigstring.create len in
    let pos = t.write buf ~pos:0 v in
    assert (pos = Bigstring.length buf);
    let str = Bigstring.to_string buf in
    Bigstring.unsafe_destroy buf;
    str
  ;;

  let to_bytes t v =
    let len = t.size v in
    let buf = Bigstring.create len in
    let pos = t.write buf ~pos:0 v in
    assert (pos = Bigstring.length buf);
    let str = Bigstring.to_bytes buf in
    Bigstring.unsafe_destroy buf;
    str
  ;;
end

module Reader = struct
  type 'a t = 'a Bin_prot.Type_class.reader =
    { read : 'a Read.reader
    ; vtag_read : (int -> 'a) Read.reader
    }

  let of_string t string =
    let buf = Bigstring.of_string string in
    let pos_ref = ref 0 in
    let v = t.read buf ~pos_ref in
    assert (!pos_ref = Bigstring.length buf);
    Bigstring.unsafe_destroy buf;
    v
  ;;

  let of_bytes t bytes =
    let buf = Bigstring.of_bytes bytes in
    let pos_ref = ref 0 in
    let v = t.read buf ~pos_ref in
    assert (!pos_ref = Bigstring.length buf);
    Bigstring.unsafe_destroy buf;
    v
  ;;
end
OCaml

Innovation. Community. Security.