Skip to content

Commit

Permalink
fix #166
Browse files Browse the repository at this point in the history
  • Loading branch information
neuecc committed Nov 9, 2023
1 parent 39a9f6c commit f6d05e6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ public partial class MyStringDictionary<TValue> : Dictionary<string, TValue>
}
```

Static constructor
---
MemoryPackable class can not define static constructor because generated partial class uses it. Instead, you can define a `static partial void StaticConstructor()` to do the same thing.

```csharp
[MemoryPackable]
public partial class CctorSample
{
static partial void StaticConstructor()
{
}
}
```

Polymorphism (Union)
---
MemoryPack supports serializing interface and abstract class objects for polymorphism serialization. In MemoryPack this feature is called Union. Only interfaces and abstracts classes are allowed to be annotated with `[MemoryPackUnion]` attributes. Unique union tags are required.
Expand Down
15 changes: 15 additions & 0 deletions sandbox/SandboxConsoleApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,26 @@

var span = CollectionsMarshal.AsSpan(value.Payload);


[MemoryPackable]
public partial class CctorSample
{
static partial void StaticConstructor()
{
}
}

[MemoryPackable]
public partial class ListBytesSample
{
public int Id { get; set; }
public List<byte> Payload { get; set; }

static partial void StaticConstructor()
{
Console.WriteLine("foo");
// throw new NotImplementedException();
}
}

[MemoryPackable]
Expand Down
5 changes: 5 additions & 0 deletions src/MemoryPack.Generator/MemoryPackGenerator.Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,12 @@ public void Emit(StringBuilder writer, IGeneratorContext context)
partial {{classOrStructOrRecord}} {{TypeName}} : IMemoryPackable<{{TypeName}}>{{fixedSizeInterface}}
{
{{EmitCustomFormatters()}}
static partial void StaticConstructor();
static {{Symbol.Name}}()
{
{{registerT}}
StaticConstructor();
}
{{fixedSizeMethod}}
[global::MemoryPack.Internal.Preserve]
Expand Down Expand Up @@ -1194,9 +1196,12 @@ string EmitGenericCollectionTemplate(IGeneratorContext context)
var code = $$"""
partial class {{TypeName}} : IMemoryPackFormatterRegister
{
static partial void StaticConstructor();
static {{Symbol.Name}}()
{
{{register}}
StaticConstructor();
}
{{staticRegisterFormatterMethod}}RegisterFormatter()
Expand Down
5 changes: 5 additions & 0 deletions src/MemoryPack.Generator/TypeScriptRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ public writeString(value: string | null): void {
return;
}
if (value.length == 0) {
this.writeCollectionHeader(0);
return;
}
// (int ~utf8-byte-count, int utf16-length, utf8-bytes)
this.ensureCapacity(8 + ((value.length + 1) * 3));
Expand Down

0 comments on commit f6d05e6

Please sign in to comment.