From 5eb8d0077a26e448ddd37709a7da0dc9e110f3d6 Mon Sep 17 00:00:00 2001 From: Vincent Bernardoff Date: Sat, 14 Jan 2017 13:13:41 +0100 Subject: [PATCH] Shell: add `Data_encoding.Binary.fixed_length` --- src/minutils/data_encoding.ml | 10 ++++++++++ src/minutils/data_encoding.mli | 3 +++ 2 files changed, 13 insertions(+) diff --git a/src/minutils/data_encoding.ml b/src/minutils/data_encoding.ml index 3de84af44..2da130d06 100644 --- a/src/minutils/data_encoding.ml +++ b/src/minutils/data_encoding.ml @@ -1178,4 +1178,14 @@ let rec length : type x. x t -> x -> int = fun e -> let to_bytes = to_bytes let length = length + + let fixed_length e = + match classify e with + | `Fixed n -> Some n + | `Dynamic | `Variable -> None + let fixed_length_exn e = + match fixed_length e with + | Some n -> n + | None -> invalid_arg "Data_encoding.Binary.fixed_length_exn" + end diff --git a/src/minutils/data_encoding.mli b/src/minutils/data_encoding.mli index be410b820..8bac5d62f 100644 --- a/src/minutils/data_encoding.mli +++ b/src/minutils/data_encoding.mli @@ -236,4 +236,7 @@ module Binary : sig val to_bytes : 'a encoding -> 'a -> MBytes.t val of_bytes : 'a encoding -> MBytes.t -> 'a option + val fixed_length : 'a encoding -> int option + val fixed_length_exn : 'a encoding -> int + end