Skip to content

Commit 9728c01

Browse files
committed
chore: Bring in Nullable impl for Net Framework
1 parent f5383a5 commit 9728c01

1 file changed

Lines changed: 193 additions & 0 deletions

File tree

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
// <copyright file="NullableAttributes.cs" company="Nonpolynomial Labs LLC">
2+
// Buttplug C# Source Code File - Visit https://buttplug.io for more info about the project.
3+
// Copyright (c) Nonpolynomial Labs LLC. All rights reserved.
4+
// Licensed under the BSD 3-Clause license. See LICENSE file in the project root for full license information.
5+
// </copyright>
6+
7+
// Nullable reference type attributes for compatibility with older frameworks.
8+
// These are only used when the compiler supports nullable reference types but
9+
// the target framework doesn't provide these attributes.
10+
// See: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/nullable-analysis
11+
12+
#if !NETSTANDARD2_1_OR_GREATER && !NETCOREAPP3_0_OR_GREATER
13+
14+
// ReSharper disable once CheckNamespace
15+
namespace System.Diagnostics.CodeAnalysis
16+
{
17+
/// <summary>
18+
/// Specifies that null is allowed as an input even if the corresponding type disallows it.
19+
/// </summary>
20+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
21+
internal sealed class AllowNullAttribute : Attribute
22+
{
23+
}
24+
25+
/// <summary>
26+
/// Specifies that null is disallowed as an input even if the corresponding type allows it.
27+
/// </summary>
28+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property, Inherited = false)]
29+
internal sealed class DisallowNullAttribute : Attribute
30+
{
31+
}
32+
33+
/// <summary>
34+
/// Specifies that an output may be null even if the corresponding type disallows it.
35+
/// </summary>
36+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
37+
internal sealed class MaybeNullAttribute : Attribute
38+
{
39+
}
40+
41+
/// <summary>
42+
/// Specifies that an output will not be null even if the corresponding type allows it.
43+
/// </summary>
44+
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
45+
internal sealed class NotNullAttribute : Attribute
46+
{
47+
}
48+
49+
/// <summary>
50+
/// Specifies that when a method returns <see cref="ReturnValue"/>, the parameter may be null even if the corresponding type disallows it.
51+
/// </summary>
52+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
53+
internal sealed class MaybeNullWhenAttribute : Attribute
54+
{
55+
/// <summary>
56+
/// Initializes the attribute with the specified return value condition.
57+
/// </summary>
58+
/// <param name="returnValue">The return value condition.</param>
59+
public MaybeNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
60+
61+
/// <summary>
62+
/// Gets the return value condition.
63+
/// </summary>
64+
public bool ReturnValue { get; }
65+
}
66+
67+
/// <summary>
68+
/// Specifies that when a method returns <see cref="ReturnValue"/>, the parameter will not be null even if the corresponding type allows it.
69+
/// </summary>
70+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
71+
internal sealed class NotNullWhenAttribute : Attribute
72+
{
73+
/// <summary>
74+
/// Initializes the attribute with the specified return value condition.
75+
/// </summary>
76+
/// <param name="returnValue">The return value condition.</param>
77+
public NotNullWhenAttribute(bool returnValue) => ReturnValue = returnValue;
78+
79+
/// <summary>
80+
/// Gets the return value condition.
81+
/// </summary>
82+
public bool ReturnValue { get; }
83+
}
84+
85+
/// <summary>
86+
/// Specifies that the output will be non-null if the named parameter is non-null.
87+
/// </summary>
88+
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue, AllowMultiple = true, Inherited = false)]
89+
internal sealed class NotNullIfNotNullAttribute : Attribute
90+
{
91+
/// <summary>
92+
/// Initializes the attribute with the associated parameter name.
93+
/// </summary>
94+
/// <param name="parameterName">The associated parameter name.</param>
95+
public NotNullIfNotNullAttribute(string parameterName) => ParameterName = parameterName;
96+
97+
/// <summary>
98+
/// Gets the associated parameter name.
99+
/// </summary>
100+
public string ParameterName { get; }
101+
}
102+
103+
/// <summary>
104+
/// Applied to a method that will never return under any circumstance.
105+
/// </summary>
106+
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
107+
internal sealed class DoesNotReturnAttribute : Attribute
108+
{
109+
}
110+
111+
/// <summary>
112+
/// Specifies that the method will not return if the associated Boolean parameter is passed the specified value.
113+
/// </summary>
114+
[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
115+
internal sealed class DoesNotReturnIfAttribute : Attribute
116+
{
117+
/// <summary>
118+
/// Initializes the attribute with the specified parameter value.
119+
/// </summary>
120+
/// <param name="parameterValue">The condition parameter value.</param>
121+
public DoesNotReturnIfAttribute(bool parameterValue) => ParameterValue = parameterValue;
122+
123+
/// <summary>
124+
/// Gets the condition parameter value.
125+
/// </summary>
126+
public bool ParameterValue { get; }
127+
}
128+
129+
/// <summary>
130+
/// Specifies that the method or property will ensure that the listed field and property members have not-null values.
131+
/// </summary>
132+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
133+
internal sealed class MemberNotNullAttribute : Attribute
134+
{
135+
/// <summary>
136+
/// Initializes the attribute with a field or property member.
137+
/// </summary>
138+
/// <param name="member">The field or property member.</param>
139+
public MemberNotNullAttribute(string member) => Members = new[] { member };
140+
141+
/// <summary>
142+
/// Initializes the attribute with field or property members.
143+
/// </summary>
144+
/// <param name="members">The field or property members.</param>
145+
public MemberNotNullAttribute(params string[] members) => Members = members;
146+
147+
/// <summary>
148+
/// Gets the field or property members.
149+
/// </summary>
150+
public string[] Members { get; }
151+
}
152+
153+
/// <summary>
154+
/// Specifies that the method or property will ensure that the listed field and property members have not-null values when returning with the specified return value condition.
155+
/// </summary>
156+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = false, AllowMultiple = true)]
157+
internal sealed class MemberNotNullWhenAttribute : Attribute
158+
{
159+
/// <summary>
160+
/// Initializes the attribute with the specified return value condition and a field or property member.
161+
/// </summary>
162+
/// <param name="returnValue">The return value condition.</param>
163+
/// <param name="member">The field or property member.</param>
164+
public MemberNotNullWhenAttribute(bool returnValue, string member)
165+
{
166+
ReturnValue = returnValue;
167+
Members = new[] { member };
168+
}
169+
170+
/// <summary>
171+
/// Initializes the attribute with the specified return value condition and field or property members.
172+
/// </summary>
173+
/// <param name="returnValue">The return value condition.</param>
174+
/// <param name="members">The field or property members.</param>
175+
public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
176+
{
177+
ReturnValue = returnValue;
178+
Members = members;
179+
}
180+
181+
/// <summary>
182+
/// Gets the return value condition.
183+
/// </summary>
184+
public bool ReturnValue { get; }
185+
186+
/// <summary>
187+
/// Gets the field or property members.
188+
/// </summary>
189+
public string[] Members { get; }
190+
}
191+
}
192+
193+
#endif

0 commit comments

Comments
 (0)