// 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() { GeneratedFirstLayout(); Console.WriteLine("PASS generated-first-layout"); } private static void GeneratedFirstLayout() { byte[] bytes = [0x02, 0x00, 0x06, 0x00, 0x00, 0x00]; // Parse returns the generated class; its properties have the C# types the layout implies. Wire.Header header = Wire.Parse(bytes); Equal((ushort)2, header.Kind); Equal(6u, header.Length); // Serialize takes the class back to bytes, and the runtime layout is still there for anything else. SequenceEqual(bytes, Wire.Serialize(header)); Equal("header", Wire.RootName); Equal(6, Wire.Layout.GetStructSizeInBytes("header")); Equal(6, Wire.Sizes.Header); } private static void Equal(T expected, T actual) { if (!EqualityComparer.Default.Equals(expected, actual)) { throw new InvalidOperationException($"Expected '{expected}', received '{actual}'."); } } private static void SequenceEqual(byte[] expected, byte[] actual) { if (!expected.AsSpan().SequenceEqual(actual)) { throw new InvalidOperationException( $"Expected {Convert.ToHexString(expected)}, received {Convert.ToHexString(actual)}."); } } // The layout text lives in the attribute; the generator adds the members of this partial class at build time. [CStructLayout("struct header { uint16 kind; uint32 length; };")] public static partial class Wire { } }