Fixpoint fold {S A}
           (f : byte -> S -> fold_state S A) (s : S) (l : list byte) :=
    match l with
    | [] => None
    | b :: l => match f b s with
                | Done a => Some (a, l)
                | More s => fold f s l
                | Error => None
                end
    end.

Lemma map_unwrap: forall A B (f: A -> B) (d: t A) bytes, unwrap (map f d) bytes = match (unwrap d bytes) with | None => None | Some (v, bytes) => Some (f v, bytes) end.