Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 638dc1c

Browse files
committed
allow borrowing from NewReference
implemented as an implicit conversion
1 parent d145ab1 commit 638dc1c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/embed_tests/References.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,20 @@ public void MoveToPyObject_SetsNull()
3636
reference.Dispose();
3737
}
3838
}
39+
40+
[Test]
41+
public void CanBorrowFromNewReference()
42+
{
43+
var dict = new PyDict();
44+
NewReference reference = Runtime.PyDict_Items(dict.Handle);
45+
try
46+
{
47+
PythonException.ThrowIfIsNotZero(Runtime.PyList_Reverse(reference));
48+
}
49+
finally
50+
{
51+
reference.Dispose();
52+
}
53+
}
3954
}
4055
}

src/runtime/NewReference.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ ref struct NewReference
1111
{
1212
IntPtr pointer;
1313

14+
[Pure]
15+
public static implicit operator BorrowedReference(in NewReference reference)
16+
=> new BorrowedReference(reference.pointer);
17+
1418
/// <summary>
1519
/// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
1620
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
@@ -36,6 +40,7 @@ public void Dispose()
3640
/// <summary>
3741
/// Creates <see cref="NewReference"/> from a raw pointer
3842
/// </summary>
43+
[Pure]
3944
public static NewReference DangerousFromPointer(IntPtr pointer)
4045
=> new NewReference {pointer = pointer};
4146

0 commit comments

Comments
 (0)