Skip to content

Commit

Permalink
Small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aragas committed Feb 25, 2025
1 parent 84abe5b commit 9a4cb61
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/BUTR.NativeAOT.Shared/SafeDataMallocHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions src/BUTR.NativeAOT.Shared/SafeStringMallocHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions src/BUTR.NativeAOT.Shared/SafeStructMallocHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public SafeStringMallocHandle ValueAsString()
throw new NativeCallException(new string(hError));
}

public TValue? ValueAsJson<TValue>(JsonTypeInfo<TValue> jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class
public TValue? ValueAsJson<TValue>(JsonTypeInfo<TValue?> jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class
{
if (typeof(TStruct) != typeof(return_value_json))
throw new Exception();
Expand Down Expand Up @@ -235,7 +235,7 @@ public void SetAsString(TaskCompletionSource<string?> tcs)
tcs.TrySetException(new NativeCallException(new string(hError)));
}

public void SetAsJson<TValue>(TaskCompletionSource<TValue?> tcs, JsonTypeInfo<TValue> jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class
public void SetAsJson<TValue>(TaskCompletionSource<TValue?> tcs, JsonTypeInfo<TValue?> jsonTypeInfo, [CallerMemberName] string? caller = null) where TValue : class
{
if (typeof(TStruct) != typeof(return_value_json))
{
Expand Down
10 changes: 5 additions & 5 deletions src/BUTR.NativeAOT.Shared/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public static SafeStringMallocHandle SerializeJsonCopy<TValue>(TValue value, Jso
public static string SerializeJson<TValue>(TValue? value, JsonTypeInfo<TValue> jsonTypeInfo)
where TValue : class => value is null ? string.Empty : JsonSerializer.Serialize(value, jsonTypeInfo);

public static TValue? DeserializeJson<TValue>(SafeStringMallocHandle json, JsonTypeInfo<TValue> jsonTypeInfo, [CallerMemberName] string? caller = null)
public static TValue? DeserializeJson<TValue>(SafeStringMallocHandle json, JsonTypeInfo<TValue?> 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<TValue>(param_json* json, JsonTypeInfo<TValue> jsonTypeInfo, [CallerMemberName] string? caller = null)
public static unsafe TValue? DeserializeJson<TValue>(param_json* json, JsonTypeInfo<TValue?> jsonTypeInfo, [CallerMemberName] string? caller = null)
where TValue : class => json is null ? null : DeserializeJson(param_json.ToSpan(json), jsonTypeInfo, caller);

private static TValue? DeserializeJson<TValue>([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, JsonTypeInfo<TValue> jsonTypeInfo, [CallerMemberName] string? caller = null)
private static TValue? DeserializeJson<TValue>([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpan<char> json, JsonTypeInfo<TValue?> jsonTypeInfo, [CallerMemberName] string? caller = null)
{
try
{
Expand All @@ -71,7 +71,7 @@ public static unsafe SafeDataMallocHandle Copy(in ReadOnlySpan<byte> data, bool
{
var dst = (byte*) Allocator.Alloc(new UIntPtr((uint) data.Length));
data.CopyTo(new Span<byte>(dst, data.Length));
return new(dst, data.Length, isOwner);
return SafeDataMallocHandle.Create(dst, data.Length, isOwner);
}

public static unsafe SafeStringMallocHandle Copy(in ReadOnlySpan<char> str, bool isOwner)
Expand All @@ -80,7 +80,7 @@ public static unsafe SafeStringMallocHandle Copy(in ReadOnlySpan<char> str, bool
var dst = (char*) Allocator.Alloc(new UIntPtr(size));
str.CopyTo(new Span<char>(dst, str.Length));
dst[str.Length] = '\0';
return new(dst, isOwner);
return SafeStringMallocHandle.Create(dst, isOwner);
}

public static unsafe SafeStructMallocHandle<TValue> Create<TValue>(TValue value, bool isOwner) where TValue : unmanaged
Expand Down

0 comments on commit 9a4cb61

Please sign in to comment.