Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,43 @@ public class Packet
public string Name { get; }
public int[] Values { get; }
}

// Serializing data into a buffer using generated 'Serialize' extension method.
var packet = new Packet { Id = 1, Name = "John", Values = new[] { 10, 20 } };
Span<byte> buffer = stackalloc byte[256];
int writtenBytes = packet.Serialize(buffer);
```

View construction does not read every property. Values are decoded directly from the original memory only on access.
> [!TIP]
> View construction does not read every property. Values are decoded directly from the original memory only on access.


## Blittable Structs

The complete serialized region is also available through implicit conversion:
Structs marked with both `[ZeroSerializer]` and `[StructLayout(LayoutKind.Sequential, Pack = 1)]` are recognized as blittable structs.

Blittable structs provide significant benefits:
- **No Offset Table**: Serialized directly as raw memory payloads without field offset table.
- **Maximum Performance**: Fast direct memory copy operations with zero overhead.

```csharp
ReadOnlySpan<byte> serializedData = packetView;
ReadOnlyMemory<byte> retainedData = packetView;
[ZeroSerializer]
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct Transform
{
public float PositionX;
public float PositionY;
public float PositionZ;
}

// Serializing a blittable struct (exact fixed byte size available via RequiredByteLength)
var transform = new Transform { PositionX = 1.0f, PositionY = 2.0f, PositionZ = 3.0f };
var buffer = new byte[TransformView.RequiredByteLength]; // 12 bytes
int writtenBytes = transform.Serialize(buffer);

// Reading via View or materializing back to the original struct
var view = new TransformView(buffer);
Transform original = view.Materialize();
```


Expand Down
Loading