@@ -5,82 +5,60 @@ namespace MemoryPack;
55public static partial class MemoryPackSerializer
66{
77 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
8- public static StateBackup BackupState ( ) => new ( true ) ;
8+ public static StateSnapshot ResetState ( bool resetReaderState = true , bool resetWriterState = true )
9+ => new ( resetReaderState , resetWriterState ) ;
910
1011 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
11- public static SerializerStateBackup BackupSerializerState ( ) => new ( true ) ;
12+ public static StateSnapshot ResetReaderState ( )
13+ => new ( true , false ) ;
1214
1315 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
14- public static DeserializerStateBackup BackupDeserializerState ( ) => new ( true ) ;
16+ public static StateSnapshot ResetWriterState ( )
17+ => new ( false , true ) ;
1518
1619 // Nested types
1720
18- public readonly struct StateBackup : IDisposable
21+ public readonly struct StateSnapshot : IDisposable
1922 {
23+ readonly bool _resetReaderState ;
24+ readonly bool _resetWriterState ;
2025 readonly SerializerWriterThreadStaticState ? _threadStaticState ;
2126 readonly MemoryPackWriterOptionalState ? _threadStaticWriterOptionalState ;
2227 readonly MemoryPackReaderOptionalState ? _threadStaticReaderOptionalState ;
23- readonly bool _isValid ;
2428
25- internal StateBackup ( bool isValid )
29+ internal StateSnapshot ( bool resetReaderState , bool resetWriterState )
2630 {
27- _threadStaticState = threadStaticState ;
28- _threadStaticWriterOptionalState = threadStaticWriterOptionalState ;
29- _threadStaticReaderOptionalState = threadStaticReaderOptionalState ;
30- _isValid = isValid ;
31+ _resetReaderState = resetReaderState ;
32+ _resetWriterState = resetWriterState ;
33+
34+ if ( resetReaderState )
35+ {
36+ _threadStaticReaderOptionalState = threadStaticReaderOptionalState ;
37+ threadStaticReaderOptionalState = null ;
38+
39+ }
40+
41+ if ( resetWriterState )
42+ {
43+ _threadStaticState = threadStaticState ;
44+ threadStaticState = null ;
45+ _threadStaticWriterOptionalState = threadStaticWriterOptionalState ;
46+ threadStaticWriterOptionalState = null ;
47+ }
3148 }
3249
3350 public void Dispose ( )
3451 {
35- if ( ! _isValid )
36- return ;
37-
38- threadStaticState = _threadStaticState ;
39- threadStaticWriterOptionalState = _threadStaticWriterOptionalState ;
40- threadStaticReaderOptionalState = _threadStaticReaderOptionalState ;
41- }
42- }
43-
44- public readonly struct SerializerStateBackup : IDisposable
45- {
46- readonly SerializerWriterThreadStaticState ? _threadStaticState ;
47- readonly MemoryPackWriterOptionalState ? _threadStaticWriterOptionalState ;
48- readonly bool _isValid ;
49-
50- internal SerializerStateBackup ( bool isValid )
51- {
52- _threadStaticState = threadStaticState ;
53- _threadStaticWriterOptionalState = threadStaticWriterOptionalState ;
54- _isValid = isValid ;
55- }
56-
57- public void Dispose ( )
58- {
59- if ( ! _isValid )
60- return ;
61-
62- threadStaticState = _threadStaticState ;
63- threadStaticWriterOptionalState = _threadStaticWriterOptionalState ;
64- }
65- }
66-
67- public readonly struct DeserializerStateBackup : IDisposable
68- {
69- readonly MemoryPackReaderOptionalState ? _threadStaticReaderOptionalState ;
70- readonly bool _isValid ;
71-
72- internal DeserializerStateBackup ( bool isValid )
73- {
74- _threadStaticReaderOptionalState = threadStaticReaderOptionalState ;
75- _isValid = isValid ;
76- }
77-
78- public void Dispose ( )
79- {
80- if ( ! _isValid )
81- return ;
82-
83- threadStaticReaderOptionalState = _threadStaticReaderOptionalState ;
52+ if ( _resetReaderState )
53+ {
54+ threadStaticReaderOptionalState = _threadStaticReaderOptionalState ;
55+ }
56+
57+ if ( _resetWriterState )
58+ {
59+ threadStaticState = _threadStaticState ;
60+ threadStaticWriterOptionalState = _threadStaticWriterOptionalState ;
61+ }
8462 }
8563 }
8664}
0 commit comments