Skip to content
Prev Previous commit
Next Next commit
BorrowedReference.Pointer is a private readonly field
  • Loading branch information
lostmsu committed Feb 13, 2020
commit 7304b25cf336264c8c078110d5418fbc2598a475
12 changes: 6 additions & 6 deletions src/runtime/BorrowedReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ namespace Python.Runtime
using System;
readonly ref struct BorrowedReference
{
public readonly IntPtr Pointer;
public bool IsNull => this.Pointer == IntPtr.Zero;
readonly IntPtr pointer;
public bool IsNull => this.pointer == IntPtr.Zero;

public PyObject ToPyObject()
{
if (this.IsNull) throw new NullReferenceException();

Runtime.XIncref(this.Pointer);
return new PyObject(this.Pointer);
Runtime.XIncref(this.pointer);
return new PyObject(this.pointer);
}

[Obsolete("Use overloads, that take BorrowedReference or NewReference")]
public IntPtr DangerousGetAddress()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a property named Address can be more fit?
Why I have to be warned if I trying use the raw pointer, seems it doesn't make any sense. Or just don't expose the interface and use explicit operator IntPtr instead.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is modeled after SafeHandle class and its DangerousGetHandle method.
The whole point of introducing these references is to get rid of IntPtrs entirely. It should warn you against converting into IntPtr. Maybe Obsolete part is unnecessary as Dangerous sounds like a warning enough. I will remove Obsolete.

=> this.IsNull ? throw new NullReferenceException() : this.Pointer;
=> this.IsNull ? throw new NullReferenceException() : this.pointer;

BorrowedReference(IntPtr pointer)
{
this.Pointer = pointer;
this.pointer = pointer;
}
}
}