Closed
Description
Follow up from #85911
Background and motivation
This proposal is about also updating Unsafe.AsPointer
to use ref readonly
. This is the same change we did for the other APIs in #85911, and it is not breaking. We didn't include AsPointer
at the time as we didn't have any use cases. This would now benefit us in CsWinRT (see #114024), as it would simplify all property accessors to get the CCW vtables for projected and custom mapped types.
API Proposal
namespace System.Runtime.CompilerServices
{
public static class Unsafe
{
- public static void* AsPointer<T>(ref T value);
+ public static void* AsPointer<T>(ref readonly T value);
}
}
API Usage
public static class SomeTypeImpl
{
[FixedAddressValueType];
private static readonly SomeTypeVtbl Vftbl;
public static nint Vftbl => (nint)Unsafe.AsPointer(in Vftbl);
static SomeTypeImpl()
{
// Initialize vtable
}
}
Risks
None. Unsafe.AsPointer
is already an unsafe API, and it's in an unsafe namespace. This API makes things simpler, but developer could already achieve the same, just by adding an Unsafe.AsRef
call on top of this chain. Just updating the signature for AsPointer
simplifies all callsites instead.