@@ -18,14 +18,12 @@ public NewReference(BorrowedReference reference, bool canBeNull = false)
1818 var address = canBeNull
1919 ? reference . DangerousGetAddressOrNull ( )
2020 : reference . DangerousGetAddress ( ) ;
21- Runtime . XIncref ( address ) ;
21+ #pragma warning disable CS0618 // Type or member is obsolete
22+ Runtime . XIncref ( reference ) ;
23+ #pragma warning restore CS0618 // Type or member is obsolete
2224 this . pointer = address ;
2325 }
2426
25- [ Pure ]
26- public static implicit operator BorrowedReference ( in NewReference reference )
27- => new BorrowedReference ( reference . pointer ) ;
28-
2927 /// <summary>
3028 /// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
3129 /// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
@@ -62,6 +60,12 @@ public IntPtr DangerousMoveToPointerOrNull()
6260 /// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
6361 /// </summary>
6462 public PyObject MoveToPyObjectOrNull ( ) => this . IsNull ( ) ? null : this . MoveToPyObject ( ) ;
63+
64+ [ Pure ]
65+ public BorrowedReference BorrowNullable ( ) => new ( pointer ) ;
66+ [ Pure ]
67+ public BorrowedReference Borrow ( ) => this . IsNull ( ) ? throw new NullReferenceException ( ) : this . BorrowNullable ( ) ;
68+
6569 /// <summary>
6670 /// Call this method to move ownership of this reference to a Python C API function,
6771 /// that steals reference passed to it.
@@ -88,14 +92,14 @@ public StolenReference Steal()
8892 /// <summary>
8993 /// Removes this reference to a Python object, and sets it to <c>null</c>.
9094 /// </summary>
95+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
9196 public void Dispose ( )
9297 {
9398 if ( this . IsNull ( ) )
9499 {
95100 return ;
96101 }
97- Runtime . XDecref ( pointer ) ;
98- pointer = IntPtr . Zero ;
102+ Runtime . XDecref ( this . Steal ( ) ) ;
99103 }
100104
101105 /// <summary>
0 commit comments