diff --git a/src/BUTR.NativeAOT.Shared/SafeDataMallocHandle.cs b/src/BUTR.NativeAOT.Shared/SafeDataMallocHandle.cs index 6b60ae6..6f0c9da 100644 --- a/src/BUTR.NativeAOT.Shared/SafeDataMallocHandle.cs +++ b/src/BUTR.NativeAOT.Shared/SafeDataMallocHandle.cs @@ -35,6 +35,8 @@ namespace BUTR.NativeAOT.Shared internal sealed unsafe class SafeDataMallocHandle : SafeHandleZeroOrMinusOneIsInvalid { + public static SafeDataMallocHandle Create(byte* ptr, int length, bool isOwner) => new(ptr, length, isOwner); + public static implicit operator param_data*(SafeDataMallocHandle handle) => (param_data*) handle.handle.ToPointer(); public static implicit operator byte*(SafeDataMallocHandle handle) => (byte*) handle.handle.ToPointer(); diff --git a/src/BUTR.NativeAOT.Shared/SafeStringMallocHandle.cs b/src/BUTR.NativeAOT.Shared/SafeStringMallocHandle.cs index a601f01..22aa056 100644 --- a/src/BUTR.NativeAOT.Shared/SafeStringMallocHandle.cs +++ b/src/BUTR.NativeAOT.Shared/SafeStringMallocHandle.cs @@ -35,6 +35,8 @@ namespace BUTR.NativeAOT.Shared internal sealed unsafe class SafeStringMallocHandle : SafeHandleZeroOrMinusOneIsInvalid { + public static SafeStringMallocHandle Create(char* ptr, bool isOwner) => new(ptr, isOwner); + public static implicit operator param_string*(SafeStringMallocHandle handle) => (param_string*) handle.handle.ToPointer(); public static implicit operator param_json*(SafeStringMallocHandle handle) => (param_json*) handle.handle.ToPointer(); public static implicit operator char*(SafeStringMallocHandle handle) => (char*) handle.handle.ToPointer(); diff --git a/src/BUTR.NativeAOT.Shared/SafeStructMallocHandle.cs b/src/BUTR.NativeAOT.Shared/SafeStructMallocHandle.cs index 9c0cde2..253aa73 100644 --- a/src/BUTR.NativeAOT.Shared/SafeStructMallocHandle.cs +++ b/src/BUTR.NativeAOT.Shared/SafeStructMallocHandle.cs @@ -97,7 +97,7 @@ public SafeStringMallocHandle ValueAsString() throw new NativeCallException(new string(hError)); } - public TValue? ValueAsJson(JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class + public TValue? ValueAsJson(JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class { if (typeof(TStruct) != typeof(return_value_json)) throw new Exception(); @@ -235,7 +235,7 @@ public void SetAsString(TaskCompletionSource tcs) tcs.TrySetException(new NativeCallException(new string(hError))); } - public void SetAsJson(TaskCompletionSource tcs, JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class + public void SetAsJson(TaskCompletionSource tcs, JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class { if (typeof(TStruct) != typeof(return_value_json)) { diff --git a/src/BUTR.NativeAOT.Shared/Utils.cs b/src/BUTR.NativeAOT.Shared/Utils.cs index 4aac294..ef22159 100644 --- a/src/BUTR.NativeAOT.Shared/Utils.cs +++ b/src/BUTR.NativeAOT.Shared/Utils.cs @@ -47,14 +47,14 @@ public static SafeStringMallocHandle SerializeJsonCopy(TValue value, Jso public static string SerializeJson(TValue? value, JsonTypeInfo jsonTypeInfo) where TValue : class => value is null ? string.Empty : JsonSerializer.Serialize(value, jsonTypeInfo); - public static TValue? DeserializeJson(SafeStringMallocHandle json, JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) + public static TValue? DeserializeJson(SafeStringMallocHandle json, JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class => json.IsInvalid ? null : DeserializeJson(json.ToSpan(), jsonTypeInfo, caller); [return: NotNullIfNotNull(nameof(json))] - public static unsafe TValue? DeserializeJson(param_json* json, JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) + public static unsafe TValue? DeserializeJson(param_json* json, JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class => json is null ? null : DeserializeJson(param_json.ToSpan(json), jsonTypeInfo, caller); - private static TValue? DeserializeJson([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan json, JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) + private static TValue? DeserializeJson([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan json, JsonTypeInfo jsonTypeInfo, [CallerMemberName] string? caller = null) { try { @@ -71,7 +71,7 @@ public static unsafe SafeDataMallocHandle Copy(in ReadOnlySpan data, bool { var dst = (byte*) Allocator.Alloc(new UIntPtr((uint) data.Length)); data.CopyTo(new Span(dst, data.Length)); - return new(dst, data.Length, isOwner); + return SafeDataMallocHandle.Create(dst, data.Length, isOwner); } public static unsafe SafeStringMallocHandle Copy(in ReadOnlySpan str, bool isOwner) @@ -80,7 +80,7 @@ public static unsafe SafeStringMallocHandle Copy(in ReadOnlySpan str, bool var dst = (char*) Allocator.Alloc(new UIntPtr(size)); str.CopyTo(new Span(dst, str.Length)); dst[str.Length] = '\0'; - return new(dst, isOwner); + return SafeStringMallocHandle.Create(dst, isOwner); } public static unsafe SafeStructMallocHandle Create(TValue value, bool isOwner) where TValue : unmanaged