// // OptionalWrapper.cs // // Copyright (c) František Boháček. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Diagnostics.CodeAnalysis; namespace NosSmooth.PacketSerializer.Abstractions; /// /// Wraps a compound value that may not be present /// and there will be nothing instead in the packet. /// The converter of underlying type will be called /// if and only if the value is not null. /// /// Whether the value is present in the packet. (to differentiate between null and not present.) /// The value. /// The underlying type. [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "Fix this, this should not happen.")] public record struct OptionalWrapper(bool Present, T? Value) { /// /// Unwrap the underlying value. /// /// The wrapper to unwrap. /// The unwrapped value. public static implicit operator T?(OptionalWrapper wrapper) { return wrapper.Value; } /// /// wrap the value in optional wrapper. /// /// The value to wrap. /// The wrapped value. public static implicit operator OptionalWrapper(T? value) { return new OptionalWrapper(true, value); } }