// 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() { GeneratedViews(); Console.WriteLine("PASS generated-views"); } private static void GeneratedViews() { byte[] bytes = [0x02, 0x00, 0x06, 0x00, 0x00, 0x00]; // A view decodes each member straight from the span when it is read; nothing is allocated for the header. var view = new Wire.HeaderView(bytes); Equal((ushort)2, view.Kind); Equal(6u, view.Length); Equal(6, view.Bytes.Length); // A view can become an object when one is wanted after all. Wire.Header header = view.ToObject(); Equal(6u, header.Length); // Too few bytes fail the way the runtime fails: the same message, offset 0, path 'header'. try { _ = new Wire.HeaderView(bytes.AsSpan(0, 4)); True(false, "a short source must fail"); } catch (CStructReadException error) { Equal("Not enough bytes: needed 6, available 4 (path 'header', offset 0).", error.Message); } } 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); } } // 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 { } }