Skip to content

Commit fc6e12c

Browse files
committed
Fix immutable collections support trivial issues.
* Change static readonly to readonly for better memory usage. * Remove redundant item serializer. * Fix error message.
1 parent 9f719a6 commit fc6e12c

3 files changed

Lines changed: 20 additions & 23 deletions

File tree

src/MsgPack/Serialization/DefaultSerializers/ImmutableCollectionSerializer`2.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace MsgPack.Serialization.DefaultSerializers
3737
internal class ImmutableCollectionSerializer<T, TItem> : MessagePackSerializer<T>
3838
where T : IEnumerable<TItem>
3939
{
40-
protected static readonly Func<TItem[], T> Factory = FindFactory();
40+
protected readonly Func<TItem[], T> Factory;
4141

4242
private static Func<TItem[], T> FindFactory()
4343
{
@@ -98,12 +98,13 @@ private static Func<TItem[], T> FindFactory()
9898
#endif // !UNITY
9999
}
100100

101-
private readonly MessagePackSerializer<TItem> _itemSerializer;
101+
protected readonly MessagePackSerializer<TItem> ItemSerializer;
102102

103103
public ImmutableCollectionSerializer( SerializationContext ownerContext, PolymorphismSchema itemsSchema )
104104
: base( ownerContext )
105105
{
106-
this._itemSerializer = ownerContext.GetSerializer<TItem>( itemsSchema );
106+
this.ItemSerializer = ownerContext.GetSerializer<TItem>( itemsSchema );
107+
this.Factory = FindFactory();
107108
}
108109

109110
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Validated by caller in base class" )]
@@ -113,7 +114,7 @@ protected internal override void PackToCore( Packer packer, T objectTree )
113114

114115
foreach ( var item in objectTree )
115116
{
116-
this._itemSerializer.PackTo( packer, item );
117+
this.ItemSerializer.PackTo( packer, item );
117118
}
118119
}
119120

@@ -136,11 +137,11 @@ protected internal override T UnpackFromCore( Unpacker unpacker )
136137
SerializationExceptions.ThrowUnexpectedEndOfStream( unpacker );
137138
}
138139

139-
buffer[ i ] = this._itemSerializer.UnpackFrom( subTreeUnpacker );
140+
buffer[ i ] = this.ItemSerializer.UnpackFrom( subTreeUnpacker );
140141
}
141142
}
142143

143-
return Factory( buffer );
144+
return this.Factory( buffer );
144145
}
145146

146147
protected internal override void UnpackToCore( Unpacker unpacker, T collection )
@@ -162,7 +163,7 @@ protected internal override async Task PackToAsyncCore( Packer packer, T objectT
162163

163164
foreach ( var item in objectTree )
164165
{
165-
await this._itemSerializer.PackToAsync( packer, item, cancellationToken ).ConfigureAwait( false );
166+
await this.ItemSerializer.PackToAsync( packer, item, cancellationToken ).ConfigureAwait( false );
166167
}
167168
}
168169

@@ -184,11 +185,11 @@ protected internal override async Task<T> UnpackFromAsyncCore( Unpacker unpacker
184185
SerializationExceptions.ThrowUnexpectedEndOfStream( unpacker );
185186
}
186187

187-
buffer[ i ] = await this._itemSerializer.UnpackFromAsync( subTreeUnpacker, cancellationToken ).ConfigureAwait( false );
188+
buffer[ i ] = await this.ItemSerializer.UnpackFromAsync( subTreeUnpacker, cancellationToken ).ConfigureAwait( false );
188189
}
189190
}
190191

191-
return Factory( buffer );
192+
return this.Factory( buffer );
192193
}
193194

194195
protected internal override Task UnpackToAsyncCore( Unpacker unpacker, T collection, CancellationToken cancellationToken )

src/MsgPack/Serialization/DefaultSerializers/ImmutableDictionarySerializer`3.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace MsgPack.Serialization.DefaultSerializers
3737
internal sealed class ImmutableDictionarySerializer<T, TKey, TValue> : MessagePackSerializer<T>
3838
where T : IEnumerable<KeyValuePair<TKey, TValue>>
3939
{
40-
private static readonly Func<KeyValuePair<TKey, TValue>[], T> _factory = FindFactory();
40+
private readonly Func<KeyValuePair<TKey, TValue>[], T> _factory;
4141

4242
private static Func<KeyValuePair<TKey, TValue>[], T> FindFactory()
4343
{
@@ -85,7 +85,7 @@ private static Func<KeyValuePair<TKey, TValue>[], T> FindFactory()
8585
CultureInfo.CurrentCulture,
8686
"'{0}' does not have CreateRange({1}[]) public static method.",
8787
factoryType.AssemblyQualifiedName,
88-
typeof( IEnumerable<KeyValuePair<TKey, TValue>> )
88+
typeof( KeyValuePair<TKey, TValue> )
8989
)
9090
);
9191
};
@@ -107,6 +107,7 @@ public ImmutableDictionarySerializer( SerializationContext ownerContext, Polymor
107107
{
108108
this._keySerializer = ownerContext.GetSerializer<TKey>( keysSchema );
109109
this._valueSerializer = ownerContext.GetSerializer<TValue>( valuesSchema );
110+
this._factory = FindFactory();
110111
}
111112

112113
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Validated by caller in base class" )]
@@ -153,7 +154,7 @@ protected internal override T UnpackFromCore( Unpacker unpacker )
153154
}
154155
}
155156

156-
return _factory( buffer );
157+
return this._factory( buffer );
157158
}
158159

159160
protected internal override void UnpackToCore( Unpacker unpacker, T collection )
@@ -211,7 +212,7 @@ protected internal override async Task<T> UnpackFromAsyncCore( Unpacker unpacker
211212
}
212213
}
213214

214-
return _factory( buffer );
215+
return this._factory( buffer );
215216
}
216217

217218
protected internal override Task UnpackToAsyncCore( Unpacker unpacker, T collection, CancellationToken cancellationToken )

src/MsgPack/Serialization/DefaultSerializers/ImmutableStackSerializer`2.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,8 @@ namespace MsgPack.Serialization.DefaultSerializers
3131
internal sealed class ImmutableStackSerializer<T, TItem> : ImmutableCollectionSerializer<T, TItem>
3232
where T : IEnumerable<TItem>
3333
{
34-
private readonly MessagePackSerializer<TItem> _itemSerializer;
35-
3634
public ImmutableStackSerializer( SerializationContext ownerContext, PolymorphismSchema itemsSchema )
37-
: base( ownerContext, itemsSchema )
38-
{
39-
this._itemSerializer = ownerContext.GetSerializer<TItem>( itemsSchema );
40-
}
35+
: base( ownerContext, itemsSchema ) { }
4136

4237
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", MessageId = "0", Justification = "Validated by caller in base class" )]
4338
protected internal override T UnpackFromCore( Unpacker unpacker )
@@ -59,11 +54,11 @@ protected internal override T UnpackFromCore( Unpacker unpacker )
5954
SerializationExceptions.ThrowUnexpectedEndOfStream( unpacker );
6055
}
6156

62-
buffer[ i ] = this._itemSerializer.UnpackFrom( subTreeUnpacker );
57+
buffer[ i ] = this.ItemSerializer.UnpackFrom( subTreeUnpacker );
6358
}
6459
}
6560

66-
return Factory( buffer );
61+
return this.Factory( buffer );
6762
}
6863

6964
#if FEATURE_TAP
@@ -87,11 +82,11 @@ protected internal override async Task<T> UnpackFromAsyncCore( Unpacker unpacker
8782
SerializationExceptions.ThrowUnexpectedEndOfStream( unpacker );
8883
}
8984

90-
buffer[ i ] = await this._itemSerializer.UnpackFromAsync( subTreeUnpacker, cancellationToken ).ConfigureAwait( false );
85+
buffer[ i ] = await this.ItemSerializer.UnpackFromAsync( subTreeUnpacker, cancellationToken ).ConfigureAwait( false );
9186
}
9287
}
9388

94-
return Factory( buffer );
89+
return this.Factory( buffer );
9590
}
9691

9792
#endif // FEATURE_TAP

0 commit comments

Comments
 (0)