Skip to content

Allow borrowing from NewReference #1088

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
allow borrowing from NewReference
implemented as an implicit conversion
  • Loading branch information
lostmsu committed Mar 11, 2020
commit 0f0362458ad2f6e7069af79b377e4308a00e111b
15 changes: 15 additions & 0 deletions src/embed_tests/References.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,20 @@ public void MoveToPyObject_SetsNull()
reference.Dispose();
}
}

[Test]
public void CanBorrowFromNewReference()
{
var dict = new PyDict();
NewReference reference = Runtime.PyDict_Items(dict.Handle);
try
{
PythonException.ThrowIfIsNotZero(Runtime.PyList_Reverse(reference));
}
finally
{
reference.Dispose();
}
}
}
}
5 changes: 5 additions & 0 deletions src/runtime/NewReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ ref struct NewReference
{
IntPtr pointer;

[Pure]
public static implicit operator BorrowedReference(in NewReference reference)
=> new BorrowedReference(reference.pointer);

/// <summary>
/// Returns <see cref="PyObject"/> wrapper around this reference, which now owns
/// the pointer. Sets the original reference to <c>null</c>, as it no longer owns it.
Expand All @@ -36,6 +40,7 @@ public void Dispose()
/// <summary>
/// Creates <see cref="NewReference"/> from a raw pointer
/// </summary>
[Pure]
public static NewReference DangerousFromPointer(IntPtr pointer)
=> new NewReference {pointer = pointer};

Expand Down