Commit f4cfd2de authored by Sydney Acksman's avatar Sydney Acksman

Remove dead HasValue code for ExtensionValue and add null-checks to ExtensionSet.Set

parent 8dc69ede
...@@ -115,6 +115,8 @@ namespace Google.Protobuf ...@@ -115,6 +115,8 @@ namespace Google.Protobuf
/// </summary> /// </summary>
public static void Set<TTarget, TValue>(ref ExtensionSet<TTarget> set, Extension<TTarget, TValue> extension, TValue value) where TTarget : IExtendableMessage<TTarget> public static void Set<TTarget, TValue>(ref ExtensionSet<TTarget> set, Extension<TTarget, TValue> extension, TValue value) where TTarget : IExtendableMessage<TTarget>
{ {
ProtoPreconditions.CheckNotNullUnconstrained(value, nameof(value));
IExtensionValue extensionValue; IExtensionValue extensionValue;
if (set == null) if (set == null)
{ {
......
...@@ -47,7 +47,6 @@ namespace Google.Protobuf ...@@ -47,7 +47,6 @@ namespace Google.Protobuf
internal sealed class ExtensionValue<T> : IExtensionValue internal sealed class ExtensionValue<T> : IExtensionValue
{ {
private bool hasValue;
private T field; private T field;
private FieldCodec<T> codec; private FieldCodec<T> codec;
...@@ -59,10 +58,6 @@ namespace Google.Protobuf ...@@ -59,10 +58,6 @@ namespace Google.Protobuf
public int CalculateSize() public int CalculateSize()
{ {
if (!hasValue)
{
return 0;
}
return codec.CalculateSizeWithTag(field); return codec.CalculateSizeWithTag(field);
} }
...@@ -70,7 +65,6 @@ namespace Google.Protobuf ...@@ -70,7 +65,6 @@ namespace Google.Protobuf
{ {
return new ExtensionValue<T>(codec) return new ExtensionValue<T>(codec)
{ {
hasValue = hasValue,
field = field is IDeepCloneable<T> ? (field as IDeepCloneable<T>).Clone() : field field = field is IDeepCloneable<T> ? (field as IDeepCloneable<T>).Clone() : field
}; };
} }
...@@ -82,7 +76,6 @@ namespace Google.Protobuf ...@@ -82,7 +76,6 @@ namespace Google.Protobuf
return other is ExtensionValue<T> return other is ExtensionValue<T>
&& codec.Equals((other as ExtensionValue<T>).codec) && codec.Equals((other as ExtensionValue<T>).codec)
&& hasValue.Equals((other as ExtensionValue<T>).hasValue)
&& Equals(field, (other as ExtensionValue<T>).field); && Equals(field, (other as ExtensionValue<T>).field);
// we check for equality in the codec since we could have equal field values however the values could be written in different ways // we check for equality in the codec since we could have equal field values however the values could be written in different ways
} }
...@@ -92,7 +85,6 @@ namespace Google.Protobuf ...@@ -92,7 +85,6 @@ namespace Google.Protobuf
unchecked unchecked
{ {
int hash = 17; int hash = 17;
hash = hash * 31 + hasValue.GetHashCode();
hash = hash * 31 + field.GetHashCode(); hash = hash * 31 + field.GetHashCode();
hash = hash * 31 + codec.GetHashCode(); hash = hash * 31 + codec.GetHashCode();
return hash; return hash;
...@@ -101,7 +93,6 @@ namespace Google.Protobuf ...@@ -101,7 +93,6 @@ namespace Google.Protobuf
public void MergeFrom(CodedInputStream input) public void MergeFrom(CodedInputStream input)
{ {
hasValue = true;
codec.ValueMerger(input, ref field); codec.ValueMerger(input, ref field);
} }
...@@ -109,24 +100,18 @@ namespace Google.Protobuf ...@@ -109,24 +100,18 @@ namespace Google.Protobuf
{ {
if (value is ExtensionValue<T>) if (value is ExtensionValue<T>)
{ {
var extensionValue = value as ExtensionValue<T>; var extensionValue = value as ExtensionValue<T>;
if (extensionValue.hasValue) codec.FieldMerger(ref field, extensionValue.field);
{
hasValue |= codec.FieldMerger(ref field, extensionValue.field);
}
} }
} }
public void WriteTo(CodedOutputStream output) public void WriteTo(CodedOutputStream output)
{ {
if (hasValue) output.WriteTag(codec.Tag);
codec.ValueWriter(output, field);
if (codec.EndTag != 0)
{ {
output.WriteTag(codec.Tag); output.WriteTag(codec.EndTag);
codec.ValueWriter(output, field);
if (codec.EndTag != 0)
{
output.WriteTag(codec.EndTag);
}
} }
} }
...@@ -134,15 +119,19 @@ namespace Google.Protobuf ...@@ -134,15 +119,19 @@ namespace Google.Protobuf
public void SetValue(T value) public void SetValue(T value)
{ {
hasValue = true;
field = value; field = value;
} }
public bool HasValue => hasValue;
public bool IsInitialized() public bool IsInitialized()
{ {
return HasValue && field is IMessage && (field as IMessage).IsInitialized(); if (field is IMessage)
{
return (field as IMessage).IsInitialized();
}
else
{
return true;
}
} }
} }
......
...@@ -254,11 +254,8 @@ namespace Google.Protobuf.Reflection ...@@ -254,11 +254,8 @@ namespace Google.Protobuf.Reflection
if (extensionValue is ExtensionValue<T>) if (extensionValue is ExtensionValue<T>)
{ {
ExtensionValue<T> single = extensionValue as ExtensionValue<T>; ExtensionValue<T> single = extensionValue as ExtensionValue<T>;
if (single.HasValue) value = single.GetValue();
{ return true;
value = single.GetValue();
return true;
}
} }
else if (extensionValue is RepeatedExtensionValue<T>) else if (extensionValue is RepeatedExtensionValue<T>)
{ {
...@@ -279,11 +276,8 @@ namespace Google.Protobuf.Reflection ...@@ -279,11 +276,8 @@ namespace Google.Protobuf.Reflection
var typeArgs = typeInfo.GenericTypeArguments; var typeArgs = typeInfo.GenericTypeArguments;
if (typeArgs.Length == 1 && typeArgs[0].GetTypeInfo().IsEnum) if (typeArgs.Length == 1 && typeArgs[0].GetTypeInfo().IsEnum)
{ {
if ((bool)typeInfo.GetDeclaredProperty(nameof(ExtensionValue<T>.HasValue)).GetValue(extensionValue)) value = (T)typeInfo.GetDeclaredMethod(nameof(ExtensionValue<T>.GetValue)).Invoke(extensionValue, EmptyParameters);
{ return true;
value = (T)typeInfo.GetDeclaredMethod(nameof(ExtensionValue<T>.GetValue)).Invoke(extensionValue, EmptyParameters);
return true;
}
} }
} }
else if (type.GetGenericTypeDefinition() == typeof(RepeatedExtensionValue<>)) else if (type.GetGenericTypeDefinition() == typeof(RepeatedExtensionValue<>))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment