123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2022 Trili Tech, <contact@trili.tech> *)(* Copyright (c) 2022 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)(* This datatype aims to represent encodings from
[Data_encoding.t]. The GADT is used to know what is the underlying
ocaml datatype. This allows to write the logic of this module in a
generic manner, as long as the tests. *)type'ocamlty=|Int64:int64ty|Int32:int32ty|Int31:intty|Int16:intty|Uint16:intty|Int8:intty|Uint8:inttyletencoding:typea.aty->aData_encoding.t=function|Int64->Data_encoding.int64|Int32->Data_encoding.int32|Int31->Data_encoding.int31|Int16->Data_encoding.int16|Uint16->Data_encoding.uint16|Int8->Data_encoding.int8|Uint8->Data_encoding.uint8letcompare:typea.aty->(moduleCompare.Swithtypet=a)=function|Int64->(moduleCompare.Int64)|Int32->(moduleCompare.Int32)|Int31->(moduleCompare.Int)|Int16->(moduleCompare.Int)|Uint16->(moduleCompare.Int)|Int8->(moduleCompare.Int)|Uint8->(moduleCompare.Int)letpp_ty:typea.Format.formatter->aty->unit=funfmtty->matchtywith|Int64->Format.fprintffmt"int64"|Int32->Format.fprintffmt"int32"|Int31->Format.fprintffmt"int31"|Int16->Format.fprintffmt"int16"|Uint16->Format.fprintffmt"uint16"|Int8->Format.fprintffmt"int8"|Uint8->Format.fprintffmt"uint8"letpp:typea.aty->Format.formatter->a->unit=function|Int64->funfmtvalue->Format.fprintffmt"%Ld"value|Int32->funfmtvalue->Format.fprintffmt"%ld"value|Int31->Format.pp_print_int|Int16->Format.pp_print_int|Uint16->Format.pp_print_int|Int8->Format.pp_print_int|Uint8->Format.pp_print_intletty_max_value:typea.aty->a=function|Int64->Int64.max_int|Int32->Int32.max_int|Int31->(1lsl30)-1|Int16->(1lsl15)-1|Uint16->(1lsl16)-1|Int8->(1lsl7)-1|Uint8->(1lsl8)-1letty_min_value:typea.aty->a=function|Int64->Int64.min_int|Int32->Int32.min_int|Int31->-(1lsl30)|Int16->-(1lsl15)|Uint16->0|Int8->-(1lsl7)|Uint8->0moduletypeBOUNDS=sigtypeocaml_typevalmin_value:ocaml_typevalmax_value:ocaml_typeendmoduletypeS=sigtypettypeocaml_typeincludeBOUNDSwithtypeocaml_type:=ocaml_typeincludeCompare.Swithtypet:=tvalencoding:tData_encoding.tvalpp:Format.formatter->t->unitvalto_value:t->ocaml_typevalof_value:ocaml_type->toptionend(* If the encoding choosen can represent strictly less values than the
underlying ocaml datatype, some exceptions could be raised at
encoding time. Those static checks aims to be executed when the
functor is instantiated to detect those cases sooner. *)letchecks(typeocaml_type)(ty:ocaml_typety)(<)(>)~min_value~max_value=letpp=pptyinifmax_value>ty_max_valuetytheninvalid_arg(Format.asprintf"Tezos-base.Bounded(%a): Maximum encodable value: %a. Bound given: %a"pp_tytypp(ty_max_valuety)ppmax_value);ifmin_value<ty_min_valuetytheninvalid_arg(Format.asprintf"Tezos-base.Bounded(%a): Minimum encodable value: %a. Bound given: %a"pp_tytypp(ty_max_valuety)ppmax_value)[@@inlinealways](* A partial encoding that ensures the decoded value is in the specified bounds. *)letguarded_encodingty~to_value~of_value=letopenData_encodinginconv_with_guardto_value(funx->matchof_valuexwithNone->Error"Out of bounds"|Somex->Okx)(encodingty)[@@inlinealways]letof_value(<)(>)~min_value~max_valuex=ifx<min_valuethenNoneelseifx>max_valuethenNoneelseSomex[@@inlinealways](* We introduce one functor by OCaml datatype so that comparison
functions are statically known and consequently inlined. Using the
GADT, we could generalise this, but OCaml (without flambda) is
unable to make the correct optimisations to inline the comparison
functions. All the business code has been factored out so only the
declaration is duplicated. *)moduleInt64(B:BOUNDSwithtypeocaml_type:=int64)=structincludeCompare.Int64includeBletto_value=Fun.idletof_value=of_value(<)(>)~min_value~max_valueletencoding=guarded_encodingInt64~to_value~of_valueletpp=ppInt64let()=checksInt64(<)(>)~min_value~max_valueendmoduleInt32(B:BOUNDSwithtypeocaml_type:=int32)=structincludeCompare.Int32includeBletto_value=Fun.idletof_value=of_value(<)(>)~min_value~max_valueletencoding=guarded_encodingInt32~to_value~of_valueletpp=ppInt32let()=checksInt32(<)(>)~min_value~max_valueend(* A specifialisation of the functor above where the interval is
restricted to the non negative integer that can be represented on
4 bytes. *)moduleNon_negative_int32=Int32(structletmin_value=0lletmax_value=Stdlib.Int32.max_intend)(* The parameter [T] of this functor allows to choose the desired
encoding without duplicating the interface. *)moduleMake31(T:sigvalty:inttyend)(B:BOUNDSwithtypeocaml_type:=int)=structincludeCompare.IntincludeBletto_value=Fun.idletof_value=of_value(<)(>)~min_value~max_valueletencoding=guarded_encodingT.ty~to_value~of_valueletpp=ppT.tylet()=checksT.ty(<)(>)~min_value~max_valueendmoduleInt31=Make31(structletty=Int31end)moduleInt16=Make31(structletty=Int16end)moduleUint16=Make31(structletty=Uint16end)moduleInt8=Make31(structletty=Int8end)moduleUint8=Make31(structletty=Uint8end)moduleInternal_for_tests=structtype'ocamlt='ocamlty=|Int64:int64t|Int32:int32t|Int31:intt|Int16:intt|Uint16:intt|Int8:intt|Uint8:inttletmin_value=ty_min_valueletmax_value=ty_max_valueletcompare=compareletpp_ty=pp_tyend