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
Faraday is a library for writing fast and memory-efficient serializers. Its
core type and related operation gives the user fine-grained control over
copying and allocation behavior while serializing user-defined types, and
presents the output in a form that makes it possible to use vectorized write
operations, such as the writev system call, or any other platform or
application-specific output APIs.
Like its sister project Angstrom, Faraday is written with network protocols
and serialization formats in mind. As such, its source distribution inclues
implementations of various RFCs that are illustrative of real-world
applications of the library. This includes a JSON serializer.
In addition, it's appropriate here to include a serializer for the simple
arithmetic expression language described in Angstrom's README.
openFaradaytype'a binop = [
| `Subof'a*'a
| `Addof'a*'a
| `Divof'a*'a
| `Mulof'a*'a
]
;;
typet = [ `Numofint | tbinop ]
letrec serialize?(prec=0)texpr=match expr with|`Numn -> write_string t (Printf.sprintf "%d" n)
|#binopasbinop ->
let prec', op, l, r =match binop with|`Sub(l, r) -> 2, '-', l, r
|`Add(l, r) -> 3, '+', l, r
|`Div(l, r) -> 4, '/', l, r
|`Mul(l, r) -> 5, '*', l, r
inif prec' < prec then write_char t '(';
serialize t ~prec:prec' l;
write_char t op;
serialize t ~prec:prec' r;
if prec' < prec then write_char t ')'letto_stringexpr=let t = create 0x1000in
serialize t expr;
serialize_to_string t
Development
To install development dependencies, pin the package from the root of the
repository: