Instance nat_Serializer : Serializer nat :=
{| serialize := nat_serialize;
deserialize := nat_deserialize;
serialize_deserialize_id := nat_serialize_deserialize_id
|}.
(* Serializer for the standard library's Fin.t based on converting to nat. *)
Definition Fin_serialize {n} (x : Fin.t n) : IOStreamWriter.t :=
serialize (proj1_sig (Fin.to_nat x)).
Definition Fin_deserialize {n} : ByteListReader.t (Fin.t n) :=
m <- deserialize ;;
match Fin.of_nat m n with
| inleft x => ByteListReader.ret x
| inright _ => ByteListReader.error
end.

Lemma Fin_of_nat_to_nat:
forall (n : nat) (a : Fin.t n), Fin.of_nat (proj1_sig (Fin.to_nat a)) n = inleft a.