diff --git a/README.md b/README.md index e80593c..ab40d43 100644 --- a/README.md +++ b/README.md @@ -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 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 serializedData = packetView; -ReadOnlyMemory 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(); ```