Repository navigation
Expand file tree
/
Copy pathBorrowedObject.cs
More file actions
37 lines (34 loc) · 1.71 KB
/
Copy pathBorrowedObject.cs
File metadata and controls
37 lines (34 loc) · 1.71 KB
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
namespace Python.Runtime.Native {
using System;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
[SuppressMessage("Design",
"CA1066:Type {0} should implement IEquatable<T> because it overrides Equals",
Justification = "ref structs can not implement interfaces, as they are not boxable")]
public ref struct BorrowedObject
{
readonly IntPtr handle;
public BorrowedObject(IntPtr handle) {
if (handle == IntPtr.Zero) throw new ArgumentNullException(nameof(handle));
this.handle = handle;
}
[Pure]
public bool Equals(BorrowedObject other) => this.handle == other.handle;
[Pure]
public override int GetHashCode() => this.handle.GetHashCode();
[Pure]
public static bool operator ==(BorrowedObject left, BorrowedObject right)
=> left.Equals(right);
[Pure]
public static bool operator !=(BorrowedObject left, BorrowedObject right)
=> !(left == right);
// ref struct can never be boxed, so it is impossible to call this via base class (object)
#pragma warning disable CS0809 // Obsolete member overrides non-obsolete member
[Obsolete(message: "This method should never be called, as BorrowedObject can not be boxed", error: true)]
[SuppressMessage(category: "Design",
checkId: "CA1065:Do not raise exceptions in unexpected locations",
Justification = "This method should never be called, which is indicated by Obsolete attribute")]
public override bool Equals(object obj) => throw new InvalidOperationException();
#pragma warning restore CS0809 // Obsolete member overrides non-obsolete member
}
}