anchor.diff:              {N, Rest}
       end.
   
  -add_field(Value, FieldDef, MsgDefs, {MsgName, Fields}) ->
  +add_field(Value, FieldDef, MsgDefs, Record) ->
       %% FIXME: what about bytes?? "For numeric types and strings, if
       %% the same value appears multiple times, the parser accepts the
       %% last value it sees." But what about bytes?
       %% http://code.google.com/apis/protocolbuffers/docs/encoding.html
       %% For now, we assume it works like strings.
  -    {MsgName,
  -     case FieldDef of
  -         #field{name = FName, occurrence = required, type = {msg, FMsgName}} ->
  -             merge_field(FName, Value, Fields, FMsgName, MsgDefs);
  -         #field{name = FName, occurrence = optional, type = {msg, FMsgName}} ->
  -             merge_field(FName, Value, Fields, FMsgName, MsgDefs);
  -         #field{name = FName, occurrence = required}->
  -             replace_field(FName, Value, Fields);
  -         #field{name = FName, occurrence = optional}->
  -             replace_field(FName, Value, Fields);
  -         #field{name = FName, occurrence = repeated} ->
  -             append_to_field(FName, Value, Fields)
  -     end}.
  -
  -merge_field(FName, NewMsg, Fields, FMsgName, MsgDefs) ->
  -    case keyfetch(FName, Fields) of
  +    case FieldDef of
  +        #field{rnum = RNum, occurrence = required, type = {msg,_FMsgName}} ->
  +            merge_field(RNum, Value, Record, MsgDefs);
  +        #field{rnum = RNum, occurrence = optional, type = {msg,_FMsgName}} ->
  +            merge_field(RNum, Value, Record, MsgDefs);
  +        #field{rnum = RNum, occurrence = required}->
  +            setelement(RNum, Record, Value);
  +        #field{rnum = RNum, occurrence = optional}->
  +            setelement(RNum, Record, Value);
  +        #field{rnum = RNum, occurrence = repeated} ->
  +            append_to_element(RNum, Value, Record)
  +    end.
  +
  +merge_field(RNum, NewMsg, Record, MsgDefs) ->
  +    case element(RNum, Record) of
           undefined ->
  -            replace_field(FName, NewMsg, Fields);
  -        {FMsgName, _PrevFields} = PrevMsg ->
  +            setelement(RNum, Record, NewMsg);
  +        PrevMsg ->
               MergedMsg = merge_msgs(PrevMsg, NewMsg, MsgDefs),
  -            replace_field(FName, MergedMsg, Fields)
  -    end.
  -
  -merge_msgs({MsgName, PrevFields}, {MsgName, NewFields}, MsgDefs) ->
  +            setelement(RNum, Record, MergedMsg)
  +    end.
  +
  +append_to_element(RNum, NewElem, Record) ->
  +    PrevElems = element(RNum, Record),
  +    setelement(RNum, Record, [NewElem | PrevElems]).
  +
  +merge_msgs(PrevMsg, NewMsg, MsgDefs)
  +  when element(1,PrevMsg) == element(1,NewMsg) ->
  +    MsgName = element(1, NewMsg),
       MsgDef = keyfetch({msg,MsgName}, MsgDefs),
  -    {MsgName,
  -     lists:foldl(
  -       fun(#field{name=FName, occurrence=repeated}, AccFields) ->
  -               PrevSeq = keyfetch(FName, AccFields),
  -               NewSeq = keyfetch(FName, NewFields),
  -               replace_field(FName, append_seqs(PrevSeq, NewSeq), AccFields);
  -          (#field{name=FName, type={msg,_FieldMsgName}}, AccFields) ->
  -               PrevMsg = keyfetch(FName,AccFields),
  -               NewMsg  = keyfetch(FName,NewFields),
  -               MergedMsg = merge_msgs(PrevMsg, NewMsg, MsgDefs),
  -               replace_field(FName, MergedMsg, AccFields);
  -          (#field{name=FName}, AccFields) ->
  -               case keyfetch(FName, NewFields) of
  -                   undefined -> AccFields;
  -                   NewValue  -> replace_field(FName, NewValue, AccFields)
  -               end
  -       end,
  -       PrevFields,
  -       MsgDef)}.
  -
  -replace_field(FName, Value, Fields) ->
  -    lists:keystore(FName, 1, Fields, {FName, Value}).
  -
  -append_to_field(FName, NewElem, Fields) ->
  -    PrevElems = keyfetch(FName, Fields),
  -    replace_field(FName, [NewElem | PrevElems], Fields).
  -
  -append_seqs(Seq1, Seq2) ->
  -    Seq1 ++ Seq2.
  +    lists:foldl(
  +      fun(#field{rnum=RNum, occurrence=repeated}, AccRecord) ->
  +              PrevSeq = element(RNum, AccRecord),
  +              NewSeq  = element(RNum, NewMsg),
  +              setelement(RNum, AccRecord, PrevSeq ++ NewSeq);
  +         (#field{rnum=RNum, type={msg,_FieldMsgName}}, AccRecord) ->
  +              PrevSubMsg = element(RNum, AccRecord),
  +              NewSubMsg  = element(RNum, NewMsg),
  +              MergedSubMsg = merge_msgs(PrevSubMsg, NewSubMsg, MsgDefs),
  +              setelement(RNum, AccRecord, MergedSubMsg);
  +         (#field{rnum=RNum}, AccRecord) ->
  +              case element(RNum, NewMsg) of
  +                  undefined -> AccRecord;
  +                  NewValue  -> setelement(RNum, AccRecord, NewValue)
  +              end
  +      end,
  +      PrevMsg,
  +      MsgDef).
  +
  +
   
   
   decode_varint(Bin) -> de_vi(Bin, 0, 0).
  
positive.code:             NewMsg = add_field(NewValue, FieldDef, MsgDefs, Msg),
              decode_field(Rest2, MsgDef, MsgDefs, NewMsg)
      end;
  decode_field(<<>>, MsgDef, _MsgDefs, {MsgName, Fields}) ->
      %% Reverse any repeated fields, but only on the top-level, not recursively.
      RepeatedFNames = [N || #field{name=N, occurrence=repeated} <- MsgDef],
      {MsgName, lists:foldl(fun(FName, Acc) ->
                                    OldValue = keyfetch(FName, Acc),
                                    NewValue = lists:reverse(OldValue),
                                    replace_field(FName, NewValue, Acc)
                            end,
                            Fields,
                            RepeatedFNames)}.
  
  decode_wiretype(0) -> varint;
  decode_wiretype(1) -> bits64;
  
negative.code: -include_lib("eunit/include/eunit.hrl").
  
  %% TODO:
  %%
  %% * Add a new_default_msg that sets default values according to
  %%   type (and optionalness) as docoumented on the google web:
  