Skip to content
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

Protocol: SafeHandle types must have a parameterless constructor. #289

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/Reader.Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Signature[] ReadArrayOfSignature()
public VariantValue[] ReadArrayOfVariantValue()
=> ReadArrayOfT<VariantValue>();

public T[] ReadArrayOfHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>() where T : SafeHandle
public T[] ReadArrayOfHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>() where T : SafeHandle, new()
=> ReadArrayOfT<T>();

[RequiresUnreferencedCode(Strings.UseNonGenericReadArray)]
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/Reader.Handle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ namespace Tmds.DBus.Protocol;

public ref partial struct Reader
{
public T? ReadHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>() where T : SafeHandle
public T? ReadHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>() where T : SafeHandle, new()
=> ReadHandleGeneric<T>();

internal T? ReadHandleGeneric<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>()
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/UnixFdCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private void ThrowDisposed()
throw new ObjectDisposedException(typeof(UnixFdCollection).FullName);
}

public T? ReadHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>(int index) where T : SafeHandle
public T? ReadHandle<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]T>(int index) where T : SafeHandle, new()
=> ReadHandleGeneric<T>(index);

// The caller of this method owns the handle and is responsible for Disposing it.
Expand Down
2 changes: 1 addition & 1 deletion src/Tmds.DBus.Protocol/VariantValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ public T? ReadHandle<
#if NET6_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
#endif
T>() where T : SafeHandle
T>() where T : SafeHandle, new()
{
EnsureTypeIs(VariantValueType.UnixFd);
return UnsafeReadHandle<T>();
Expand Down
8 changes: 4 additions & 4 deletions src/Tmds.DBus.Tool/ProtocolGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Xml.Linq;
using Microsoft.CodeAnalysis;
using Tmds.DBus.Protocol;
using Microsoft.Win32.SafeHandles;

namespace Tmds.DBus.Tool
{
Expand Down Expand Up @@ -67,7 +68,6 @@ public bool TryGenerate(IEnumerable<InterfaceDescription> interfaceDescriptions,

AppendLine($"using System;");
AppendLine($"using Tmds.DBus.Protocol;");
AppendLine($"using SafeHandle = System.Runtime.InteropServices.SafeHandle;");
AppendLine($"using System.Collections.Generic;");
AppendLine("using System.Threading.Tasks;");

Expand Down Expand Up @@ -899,7 +899,7 @@ private string CallReadArgumentType(string signature)
case "v":
return $"reader.{nameof(Reader.ReadVariantValue)}()";
case "h":
return $"reader.{nameof(Reader.ReadHandle)}<{typeof(SafeHandle).FullName}>()";
return $"reader.{nameof(Reader.ReadHandle)}<{typeof(SafeFileHandle).FullName}>()";

case "ay":
return $"reader.{nameof(Reader.ReadArrayOfByte)}()";
Expand Down Expand Up @@ -928,7 +928,7 @@ private string CallReadArgumentType(string signature)
case "av":
return $"reader.{nameof(Reader.ReadArrayOfVariantValue)}()";
case "ah":
return $"reader.{nameof(Reader.ReadArrayOfHandle)}<{typeof(SafeHandle).FullName}>()";
return $"reader.{nameof(Reader.ReadArrayOfHandle)}<{typeof(SafeFileHandle).FullName}>()";

case "a{sv}":
return $"reader.{nameof(Reader.ReadDictionaryOfStringToVariantValue)}()";
Expand Down Expand Up @@ -1012,7 +1012,7 @@ private static string GetDotnetType(DBusType type, ReadOnlySpan<byte> innerSigna
case DBusType.Variant:
return readNotWrite ? "VariantValue" : "Variant";
case DBusType.UnixFd:
return "SafeHandle";
return typeof(SafeHandle).FullName;

case DBusType.Array:
{
Expand Down
Loading