Skip to content

Commit

Permalink
Make Field & Tag members readonly where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
ReubenBond committed Aug 25, 2023
1 parent 6a4cc90 commit 2f9cef7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 29 deletions.
29 changes: 14 additions & 15 deletions src/Orleans.Serialization/WireProtocol/Field.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public uint FieldIdDelta
// If the embedded field id delta is valid, return it, otherwise return the extended field id delta.
// The extended field id might not be valid if this field has the Extended wire type.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
readonly get
{
#if DEBUG
if (!HasFieldId) throw new FieldIdNotPresentException();
Expand Down Expand Up @@ -116,7 +116,7 @@ public Type FieldType
/// Gets a value indicating whether this instance has a field identifier.
/// </summary>
/// <value><see langword="true" /> if this instance has a field identifier; otherwise, <see langword="false" />.</value>
public bool HasFieldId
public readonly bool HasFieldId
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => !Tag.HasExtendedWireType;
Expand All @@ -126,7 +126,7 @@ public bool HasFieldId
/// Gets a value indicating whether this instance has an extended field identifier.
/// </summary>
/// <value><see langword="true" /> if this instance has an extended field identifier; otherwise, <see langword="false" />.</value>
public bool HasExtendedFieldId
public readonly bool HasExtendedFieldId
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Tag.HasExtendedFieldId;
Expand All @@ -137,8 +137,7 @@ public bool HasExtendedFieldId
/// </summary>
/// <value>The wire type.</value>
public WireType WireType
{
get => Tag.WireType;
{ readonly get => Tag.WireType;
set => Tag.WireType = value;
}

Expand All @@ -148,7 +147,7 @@ public WireType WireType
/// <value>The schema type.</value>
public SchemaType SchemaType
{
get
readonly get
{
#if DEBUG
if (!IsSchemaTypeValid)
Expand All @@ -169,7 +168,7 @@ public SchemaType SchemaType
/// <value>The extended wire type.</value>
public ExtendedWireType ExtendedWireType
{
get
readonly get
{
#if DEBUG
if (WireType != WireType.Extended)
Expand All @@ -186,31 +185,31 @@ public ExtendedWireType ExtendedWireType
/// Gets a value indicating whether this instance has a valid schema type.
/// </summary>
/// <value><see langword="true" /> if this instance has a valid schema; otherwise, <see langword="false" />.</value>
public bool IsSchemaTypeValid => Tag.IsSchemaTypeValid;
public readonly bool IsSchemaTypeValid => Tag.IsSchemaTypeValid;

/// <summary>
/// Gets a value indicating whether this instance has an extended schema type.
/// </summary>
/// <value><see langword="true" /> if this instance has an extended schema type; otherwise, <see langword="false" />.</value>
public bool HasExtendedSchemaType => Tag.IsSchemaTypeValid && Tag.SchemaType != SchemaType.Expected;
public readonly bool HasExtendedSchemaType => Tag.IsSchemaTypeValid && Tag.SchemaType != SchemaType.Expected;

/// <summary>
/// Gets a value indicating whether this instance represents the end of base fields in a tag-delimited structure.
/// </summary>
/// <value><see langword="true" /> if this instance represents end of base fields in a tag-delimited structure; otherwise, <see langword="false" />.</value>
public bool IsEndBaseFields => Tag.IsEndBaseFields;
public readonly bool IsEndBaseFields => Tag.IsEndBaseFields;

/// <summary>
/// Gets a value indicating whether this instance represents the end of a tag-delimited structure.
/// </summary>
/// <value><see langword="true" /> if this instance represents end of a tag-delimited structure; otherwise, <see langword="false" />.</value>
public bool IsEndObject => Tag.IsEndObject;
public readonly bool IsEndObject => Tag.IsEndObject;

/// <summary>
/// Gets a value indicating whether this instance represents the end of a tag-delimited structure or the end of base fields in a tag-delimited structure.
/// </summary>
/// <value><see langword="true" /> if this instance represents the end of a tag-delimited structure or the end of base fields in a tag-delimited structure; otherwise, <see langword="false" />.</value>
public bool IsEndBaseOrEndObject
public readonly bool IsEndBaseOrEndObject
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Tag.HasExtendedWireType/* && Tag.ExtendedWireType <= ExtendedWireType.EndBaseFields*/;
Expand All @@ -219,7 +218,7 @@ public bool IsEndBaseOrEndObject
/// <summary>
/// Gets a value indicating whether this instance has a wire type of <see cref="WireType.Reference"/>.
/// </summary>
public bool IsReference => Tag.WireType == WireType.Reference;
public readonly bool IsReference => Tag.WireType == WireType.Reference;

/// <summary>
/// Ensures that the wire type is <see cref="WireType.TagDelimited"/>.
Expand Down Expand Up @@ -281,7 +280,7 @@ public override string ToString()
return builder.ToStringAndClear();
#else
var builder = new StringBuilder();
builder.Append("[");
builder.Append('[');
builder.Append(WireType);

if (HasFieldId)
Expand All @@ -308,7 +307,7 @@ public override string ToString()
builder.Append(ExtendedWireType);
}

builder.Append("]");
builder.Append(']');
return builder.ToString();
#endif
}
Expand Down
27 changes: 13 additions & 14 deletions src/Orleans.Serialization/WireProtocol/Tag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public struct Tag
internal Tag(uint tag) => _tag = tag;

/// <summary>
/// Performs an implicit conversion from <see cref="System.Byte"/> to <see cref="Tag"/>.
/// Performs an implicit conversion from <see cref="byte"/> to <see cref="Tag"/>.
/// </summary>
/// <param name="tag">The tag.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Tag(byte tag) => new Tag(tag);
public static implicit operator Tag(byte tag) => new(tag);

/// <summary>
/// Performs an implicit conversion from <see cref="Tag"/> to <see cref="System.Byte"/>.
/// Performs an implicit conversion from <see cref="Tag"/> to <see cref="byte"/>.
/// </summary>
/// <param name="tag">The tag.</param>
/// <returns>The result of the conversion.</returns>
Expand All @@ -69,8 +69,7 @@ public struct Tag
/// Gets or sets the wire type of the data following this tag.
/// </summary>
public WireType WireType
{
get => (WireType)(_tag & WireTypeMask);
{ readonly get => (WireType)(_tag & WireTypeMask);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => _tag = (_tag & ~(uint)WireTypeMask) | ((uint)value & WireTypeMask);
}
Expand All @@ -79,7 +78,7 @@ public WireType WireType
/// Gets a value indicating whether this instance has an extended wire type.
/// </summary>
/// <value><see langword="true" /> if this instance has an extended wire type; otherwise, <see langword="false" />.</value>
public bool HasExtendedWireType
public readonly bool HasExtendedWireType
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _tag >= (byte)WireType.Extended; //(this.tag & (byte) WireType.Extended) == (byte) WireType.Extended;
Expand All @@ -91,23 +90,23 @@ public bool HasExtendedWireType
public ExtendedWireType ExtendedWireType
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (ExtendedWireType)(_tag & ExtendedWireTypeMask);
readonly get => (ExtendedWireType)(_tag & ExtendedWireTypeMask);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => _tag = (_tag & ~(uint)ExtendedWireTypeMask) | ((uint)value & ExtendedWireTypeMask);
}

internal bool IsEndBaseFields => _tag == ((byte)WireType.Extended | (byte)ExtendedWireType.EndBaseFields);
internal readonly bool IsEndBaseFields => _tag == ((byte)WireType.Extended | (byte)ExtendedWireType.EndBaseFields);

internal bool IsEndObject => _tag == ((byte)WireType.Extended | (byte)ExtendedWireType.EndTagDelimited);
internal readonly bool IsEndObject => _tag == ((byte)WireType.Extended | (byte)ExtendedWireType.EndTagDelimited);

/// <summary>
/// Gets or sets the schema type.
/// </summary>
public SchemaType SchemaType
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (SchemaType)(_tag & SchemaTypeMask);
readonly get => (SchemaType)(_tag & SchemaTypeMask);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => _tag = (_tag & ~(uint)SchemaTypeMask) | ((uint)value & SchemaTypeMask);
Expand All @@ -117,7 +116,7 @@ public SchemaType SchemaType
/// Gets a value indicating whether the <see cref="SchemaType" /> property is valid.
/// </summary>
/// <value><see langword="true"/> if the <see cref="SchemaType"/> is valid, <see langword="false"/> otherwise.</value>
public bool IsSchemaTypeValid
public readonly bool IsSchemaTypeValid
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => !HasExtendedWireType; //(this.tag & (byte) WireType.Extended) != (byte) WireType.Extended;
Expand All @@ -132,7 +131,7 @@ public bool IsSchemaTypeValid
public uint FieldIdDelta
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _tag & FieldIdMask;
readonly get => _tag & FieldIdMask;

[MethodImpl(MethodImplOptions.AggressiveInlining)]
set => _tag = (_tag & ~(uint)FieldIdMask) | (value & FieldIdMask);
Expand All @@ -154,7 +153,7 @@ public uint FieldIdDelta
/// If all bits are set in the field id portion of the tag, this field id is not valid and this tag must be followed by a field id.
/// Therefore, field ids 0-6 can be represented without additional bytes.
/// </remarks>
public bool IsFieldIdValid
public readonly bool IsFieldIdValid
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (_tag & FieldIdCompleteMask) != FieldIdCompleteMask && !HasExtendedWireType;
Expand All @@ -163,7 +162,7 @@ public bool IsFieldIdValid
/// <summary>
/// Gets a value indicating whether the tag is followed by an extended field id.
/// </summary>
public bool HasExtendedFieldId
public readonly bool HasExtendedFieldId
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => (_tag & FieldIdCompleteMask) == FieldIdCompleteMask && !HasExtendedWireType;
Expand Down

0 comments on commit 2f9cef7

Please sign in to comment.