~ruther/NosSmooth

NosSmooth/Packets/NosSmooth.PacketSerializer.Abstractions/NullableWrapper.cs -rw-r--r-- 1.4 KiB
8e5e00cc — František Boháček chore: bump packet versions 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
//  NullableWrapper.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;

/// <summary>
/// Wraps a compound value that may not be present
/// and there will be "-1" instead in the packet.
/// The converter of underlying type will be called
/// if and only if the value is not null.
/// </summary>
/// <param name="Value">The value.</param>
/// <typeparam name="T">The underlying type.</typeparam>
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1313:Parameter names should begin with lower-case letter", Justification = "Fix this, this should not happen.")]
public record struct NullableWrapper<T>(T? Value)
{
    /// <summary>
    /// Unwrap the underlying value.
    /// </summary>
    /// <param name="wrapper">The wrapper to unwrap.</param>
    /// <returns>The unwrapped value.</returns>
    public static implicit operator T?(NullableWrapper<T> wrapper)
    {
        return wrapper.Value;
    }

    /// <summary>
    /// Wrap the value in nullable wrapper.
    /// </summary>
    /// <param name="value">The value to wrap.</param>
    /// <returns>The wrapped value.</returns>
    public static implicit operator NullableWrapper<T>(T? value)
    {
        return new NullableWrapper<T>(value);
    }
}
Do not follow this link