🌐 AI搜索 & 代理 主页
Skip to content
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
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