// Generated from executable documentation examples. Edit the source region, then regenerate. using System; using System.IO; using System.IO.Pipelines; using System.Linq; using System.Buffers; using System.Collections.Generic; using System.Dynamic; using System.Globalization; using System.Numerics; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; using CStructSharp; using CStructSharp.Codecs; using CStructSharp.Diagnostics; using CStructSharp.Introspection; using CStructSharp.Values; internal static partial class Program { public static void Main() { DecodeHeader(); Console.WriteLine("PASS decode-header"); } private static void DecodeHeader() { var layout = new CStruct("struct header { uint16 kind; uint32 length; };"); ReadOnlySpan bytes = [0x02, 0x00, 0x06, 0x00, 0x00, 0x00]; StructValue header = layout.Parse(bytes, "header"); Equal((ushort)2, header.Get("kind")); Equal(6U, header.Get("length")); bool read = layout.TryReadValue
(bytes, "header", out Header? typed); True(read && typed is { Kind: 2, Length: 6 }, "Typed header result differed."); True(!layout.TryReadValue
(bytes[..1], "header", out _), "Truncated TryReadValue should fail."); } private static void Equal(T expected, T actual) { if (!EqualityComparer.Default.Equals(expected, actual)) { throw new InvalidOperationException($"Expected '{expected}', received '{actual}'."); } } private static void True(bool condition, string message) { if (!condition) { throw new InvalidOperationException(message); } } public sealed class Header : ICStructMapped
{ public ushort Kind { get; set; } public uint Length { get; set; } public static Header ReadFrom(StructValue source) { return new Header { Kind = source.Get("kind"), Length = source.Get("length") }; } public static void WriteTo(Header value, StructValue target) { target["kind"] = value.Kind; target["length"] = value.Length; } [ModuleInitializer] internal static void Register() { MappedTypes.Register
(); } } #region api-guide-map-mapped-type // The generator writes ReadFrom, WriteTo, and the registration for this partial class. [CStructMapped] public sealed partial class MappedPoint { public short X { get; set; } public short Y { get; set; } } #endregion }