-
-
Notifications
You must be signed in to change notification settings - Fork 434
Update API to use Native-Sized Integers #387
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
Comments
Note, as per the proposal, the following pointer members are not exposed on the corresponding native-sized integer: // constructors
// arithmetic operators
// implicit and explicit conversions
public static readonly IntPtr Zero; // use 0 instead
public static int Size { get; } // use sizeof() instead
public static IntPtr Add(IntPtr pointer, int offset); // use + operator instead
public static IntPtr Subtract(IntPtr pointer, int offset); // use - operator instead
public int ToInt32(); // cast to int instead
public long ToInt64(); // cast to long instead
public void* ToPointer(); However, implicit casting (actually there is no cast performed in reality as the implementation is syntatic sugar) comes to the rescue in most use cases, e.g.: nint a = IntPtr.Zero; // Existing code works when assigning to nint
((IntPtr)a).ToPointer(); // A cast is needed here - though is is removed on compilation. Because of these changes then any existing C#9.0 code that expects to receive a pointer from the API, but instead gets the new native-sized integer, and is using implicit typing, the code will fail to compile if they try to access any of e.g. // Implicit typed a used to be IntPtr, but is now nint
var a = APIMethodPreviousReturnedIntPtr();
var b = a.ToInt32(); // No longer compiles
// Or direct access to return value
b = APIMethodPreviousReturnedIntPtr().ToInt32(); // No longer compiles
IntPtr c = APIMethodPreviousReturnedIntPtr(); // Implicit 'cast'
b = c.ToInt32(); // Succeeds
b = ((IntPtr)APIMethodPreviousReturnedIntPtr()).ToInt32(); // Succeeds (Similar for ConclusionC#9.0 was only released last month, and |
* nints and start of Vulkan Function Tables (closes #387) * Missed a bit * Add {RESULT} * Vulkan function tables * XR function tables and build fixes * Fix build How many times am I gonna do this? * Use Regex for faster Substitution * Update release notes, update NativePackages Co-authored-by: Kai Jellinghaus <contact@kaij.tech> Co-authored-by: Craig Dean <thargy@yahoo.com>
Uh oh!
There was an error while loading. Please reload this page.
Summary of feature
C# 9 added support for native-sized integers as a drop-in replacements for
IntPtr
(andUIntPtr
). The new native-sized intger types,nint
andnuint
, were added specifically to improve the experience of coding for interop scenarios, e.g. by low-level library authors, of whichSilk.Net
is a prime example. By changing our references toIntPtr
andUIntPtr
tonint
andnuint
respectively, consumers using C#9.0 will be pointed towards the new types and will benefit from the improvements provided.Further,
Silk.Net
internal development will benefit by being less verbose.Comments
After reviewing the implementation, the 2 new types are effectively implemented as syntatic sugar over
IntPr
andUIntPtr
, with a new attributeNativeIntegerAttribute
marking instances of the type for code compiled using C#9.0+. As such, library consumers on an older version of C# should continue to see the API as exposingIntPtr
andUIntPtr
and there shouldn't be any breaking changes introduced.Following this change, code such as:
So long as the consumer is using C#9.0+.
Note this code can be called using IntPtr's due to implicit casting, e.g.:
Will continue to work, allowing the API to be backwards compatible.
It would be useful to introduce the change in a preview build, after initial careful testing, to see if it causes any issues for existing consumers.
Alternatives
It is also plausible to not make this change at all. Consumers can then optionally perform the casting themselves. The only downside is the reduced discoverability of the new modern types, as there shouldn't be any real performance impact of casting, for example, the following is valid:
The text was updated successfully, but these errors were encountered: