From b4c2409a001ed58b91ff9ac8d6b53d80f58f74f3 Mon Sep 17 00:00:00 2001 From: Thomas Ibel Date: Sun, 24 Aug 2025 08:25:00 +0200 Subject: [PATCH] Protocol: remove obsoletes --- src/Tmds.DBus.Protocol/Array.cs | 9 - src/Tmds.DBus.Protocol/Connection.cs | 4 - src/Tmds.DBus.Protocol/Dict.cs | 8 - src/Tmds.DBus.Protocol/MessageWriter.Array.cs | 9 - .../MessageWriter.Dictionary.cs | 18 - .../MessageWriter.Variant.cs | 5 - .../MessageWriter.WriteT.cs | 4 - src/Tmds.DBus.Protocol/ObjectPath.cs | 2 - src/Tmds.DBus.Protocol/Reader.Dictionary.cs | 4 - src/Tmds.DBus.Protocol/Signature.cs | 6 - src/Tmds.DBus.Protocol/Struct.cs | 63 --- src/Tmds.DBus.Protocol/TypeModel.cs | 13 - src/Tmds.DBus.Protocol/Variant.cs | 454 ------------------ src/Tmds.DBus.Protocol/VariantExtensions.cs | 41 -- src/Tmds.DBus.Protocol/VariantValue.cs | 5 +- .../ExceptionTests.cs | 6 +- test/Tmds.DBus.Protocol.Tests/ReaderTests.cs | 2 - test/Tmds.DBus.Protocol.Tests/WriterTests.cs | 62 --- 18 files changed, 4 insertions(+), 711 deletions(-) delete mode 100644 src/Tmds.DBus.Protocol/Variant.cs delete mode 100644 src/Tmds.DBus.Protocol/VariantExtensions.cs diff --git a/src/Tmds.DBus.Protocol/Array.cs b/src/Tmds.DBus.Protocol/Array.cs index 61707893..80a1eeba 100644 --- a/src/Tmds.DBus.Protocol/Array.cs +++ b/src/Tmds.DBus.Protocol/Array.cs @@ -2,9 +2,6 @@ namespace Tmds.DBus.Protocol; -// Using obsolete generic write members -#pragma warning disable CS0618 - public sealed class Array : IDBusWritable, IList, IVariantValueConvertable where T : notnull { @@ -89,12 +86,6 @@ public bool Remove(T item) return _values.Remove(item); } - public Variant AsVariant() - => Variant.FromArray(this); - - public static implicit operator Variant(Array value) - => value.AsVariant(); - public static implicit operator VariantValue(Array value) => value.AsVariantValue(); diff --git a/src/Tmds.DBus.Protocol/Connection.cs b/src/Tmds.DBus.Protocol/Connection.cs index 164d908a..d1b6e872 100644 --- a/src/Tmds.DBus.Protocol/Connection.cs +++ b/src/Tmds.DBus.Protocol/Connection.cs @@ -250,10 +250,6 @@ private void RefHandles(MessageBuffer message) message.RefHandles(); } - [Obsolete("Use an overload that accepts ObserverFlags.")] - public ValueTask AddMatchAsync(MatchRule rule, MessageValueReader reader, Action handler, object? readerState = null, object? handlerState = null, bool emitOnCapturedContext = true, bool subscribe = true) - => AddMatchAsync(rule, reader, handler, readerState, handlerState, emitOnCapturedContext, ObserverFlags.EmitOnDispose | (!subscribe ? ObserverFlags.NoSubscribe : default)); - public ValueTask AddMatchAsync(MatchRule rule, MessageValueReader reader, Action handler, ObserverFlags flags, object? readerState = null, object? handlerState = null, bool emitOnCapturedContext = true) => AddMatchAsync(rule, reader, handler, readerState, handlerState, emitOnCapturedContext, flags); diff --git a/src/Tmds.DBus.Protocol/Dict.cs b/src/Tmds.DBus.Protocol/Dict.cs index a8223ae5..483d4825 100644 --- a/src/Tmds.DBus.Protocol/Dict.cs +++ b/src/Tmds.DBus.Protocol/Dict.cs @@ -2,9 +2,6 @@ namespace Tmds.DBus.Protocol; -// Using obsolete generic write members -#pragma warning disable CS0618 - public sealed class Dict : IDBusWritable, IDictionary, IVariantValueConvertable where TKey : notnull where TValue : notnull @@ -26,11 +23,6 @@ private Dict(Dictionary value) _dict = value; } - public Variant AsVariant() => Variant.FromDict(this); - - public static implicit operator Variant(Dict value) - => value.AsVariant(); - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026")] // this is a supported variant type. void IDBusWritable.WriteTo(ref MessageWriter writer) => writer.WriteDictionary(_dict); diff --git a/src/Tmds.DBus.Protocol/MessageWriter.Array.cs b/src/Tmds.DBus.Protocol/MessageWriter.Array.cs index efb0ffc9..5047de71 100644 --- a/src/Tmds.DBus.Protocol/MessageWriter.Array.cs +++ b/src/Tmds.DBus.Protocol/MessageWriter.Array.cs @@ -110,15 +110,6 @@ public void WriteArray(ReadOnlySpan value) public void WriteArray(IEnumerable value) => WriteArrayOfT(value); - public void WriteArray(Variant[] value) - => WriteArray(value.AsSpan()); - - public void WriteArray(ReadOnlySpan value) - => WriteArrayOfT(value); - - public void WriteArray(IEnumerable value) - => WriteArrayOfT(value); - public void WriteArray(SafeHandle[] value) => WriteArray(value.AsSpan()); diff --git a/src/Tmds.DBus.Protocol/MessageWriter.Dictionary.cs b/src/Tmds.DBus.Protocol/MessageWriter.Dictionary.cs index 9d8dcb9c..7efb57d6 100644 --- a/src/Tmds.DBus.Protocol/MessageWriter.Dictionary.cs +++ b/src/Tmds.DBus.Protocol/MessageWriter.Dictionary.cs @@ -1,8 +1,5 @@ namespace Tmds.DBus.Protocol; -// Using obsolete generic write members -#pragma warning disable CS0618 - public ref partial struct MessageWriter { public ArrayStart WriteDictionaryStart() @@ -29,21 +26,6 @@ public void WriteDictionary(KeyValuePair[] value) public void WriteDictionary(Dictionary value) => WriteDictionary(value); - // Write method for the common 'a{sv}' type. - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026")] // It's safe to call WriteDictionary with these types. - public void WriteDictionary(IEnumerable> value) - => WriteDictionary(value); - - // Write method for the common 'a{sv}' type. - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026")] // It's safe to call WriteDictionary with these types. - public void WriteDictionary(KeyValuePair[] value) - => WriteDictionary(value); - - // Write method for the common 'a{sv}' type. - [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026")] // It's safe to call WriteDictionary with these types. - public void WriteDictionary(Dictionary value) - => WriteDictionary(value); - private void WriteDictionary(IEnumerable> value) where TKey : notnull where TValue : notnull diff --git a/src/Tmds.DBus.Protocol/MessageWriter.Variant.cs b/src/Tmds.DBus.Protocol/MessageWriter.Variant.cs index 60f1dcd2..8a57c0db 100644 --- a/src/Tmds.DBus.Protocol/MessageWriter.Variant.cs +++ b/src/Tmds.DBus.Protocol/MessageWriter.Variant.cs @@ -2,11 +2,6 @@ namespace Tmds.DBus.Protocol; public ref partial struct MessageWriter { - public void WriteVariant(Variant value) - { - value.WriteTo(ref this); - } - public void WriteVariant(VariantValue value) { value.WriteVariantTo(ref this); diff --git a/src/Tmds.DBus.Protocol/MessageWriter.WriteT.cs b/src/Tmds.DBus.Protocol/MessageWriter.WriteT.cs index f4d49373..1ef41361 100644 --- a/src/Tmds.DBus.Protocol/MessageWriter.WriteT.cs +++ b/src/Tmds.DBus.Protocol/MessageWriter.WriteT.cs @@ -53,10 +53,6 @@ internal void Write(T value) where T : notnull { WriteSignature(((Signature)(object)value)); } - else if (typeof(T) == typeof(Variant)) - { - ((Variant)(object)value).WriteTo(ref this); - } else if (typeof(T) == typeof(VariantValue)) { ((VariantValue)(object)value).WriteVariantTo(ref this); diff --git a/src/Tmds.DBus.Protocol/ObjectPath.cs b/src/Tmds.DBus.Protocol/ObjectPath.cs index 850383e5..34c3235a 100644 --- a/src/Tmds.DBus.Protocol/ObjectPath.cs +++ b/src/Tmds.DBus.Protocol/ObjectPath.cs @@ -28,6 +28,4 @@ private void ThrowEmptyException() public static implicit operator string(ObjectPath value) => value._value; public static implicit operator ObjectPath(string value) => new ObjectPath(value); - - public Variant AsVariant() => new Variant(this); } \ No newline at end of file diff --git a/src/Tmds.DBus.Protocol/Reader.Dictionary.cs b/src/Tmds.DBus.Protocol/Reader.Dictionary.cs index 6311dcbc..93a08ee8 100644 --- a/src/Tmds.DBus.Protocol/Reader.Dictionary.cs +++ b/src/Tmds.DBus.Protocol/Reader.Dictionary.cs @@ -1,9 +1,5 @@ namespace Tmds.DBus.Protocol; - -// Using obsolete generic read members -#pragma warning disable CS0618 - public ref partial struct Reader { public ArrayEnd ReadDictionaryStart() diff --git a/src/Tmds.DBus.Protocol/Signature.cs b/src/Tmds.DBus.Protocol/Signature.cs index 100cb9db..fbc69ab3 100644 --- a/src/Tmds.DBus.Protocol/Signature.cs +++ b/src/Tmds.DBus.Protocol/Signature.cs @@ -22,10 +22,6 @@ public readonly struct Signature internal byte[] Data => _value ?? Array.Empty(); - [Obsolete("Use the constructor that accepts a ReadOnlySpan.")] - public Signature(string value) - => _value = Encoding.UTF8.GetBytes(value); - public Signature(ReadOnlySpan value) => _value = value.ToArray(); @@ -34,6 +30,4 @@ public override string ToString() public static implicit operator Signature(ReadOnlySpan value) => new Signature(value); - - public Variant AsVariant() => new Variant(this); } \ No newline at end of file diff --git a/src/Tmds.DBus.Protocol/Struct.cs b/src/Tmds.DBus.Protocol/Struct.cs index 019c9689..73d7dc1f 100644 --- a/src/Tmds.DBus.Protocol/Struct.cs +++ b/src/Tmds.DBus.Protocol/Struct.cs @@ -1,8 +1,5 @@ namespace Tmds.DBus.Protocol; -// Using obsolete generic write members -#pragma warning disable CS0618 - public static class Struct { public static Struct Create(T1 item1) @@ -109,12 +106,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private ValueTuple ToValueTuple() => new ValueTuple(Item1); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -149,12 +140,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2) ToValueTuple() => (Item1, Item2); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -192,12 +177,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2, T3) ToValueTuple() => (Item1, Item2, Item3); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -239,12 +218,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2, T3, T4) ToValueTuple() => (Item1, Item2, Item3, Item4); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -290,12 +263,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2, T3, T4, T5) ToValueTuple() => (Item1, Item2, Item3, Item4, Item5); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -345,12 +312,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2, T3, T4, T5, T6) ToValueTuple() => (Item1, Item2, Item3, Item4, Item5, Item6); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -404,12 +365,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2, T3, T4, T5, T6, T7) ToValueTuple() => (Item1, Item2, Item3, Item4, Item5, Item6, Item7); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -467,12 +422,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2, T3, T4, T5, T6, T7, T8) ToValueTuple() => (Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -534,12 +483,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2, T3, T4, T5, T6, T7, T8, T9) ToValueTuple() => (Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); @@ -605,12 +548,6 @@ void IDBusWritable.WriteTo(ref MessageWriter writer) private (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) ToValueTuple() => (Item1, Item2, Item3, Item4, Item5, Item6, Item7, Item8, Item9, Item10); - public Variant AsVariant() - => Variant.FromStruct(this); - - public static implicit operator Variant(Struct value) - => value.AsVariant(); - public static implicit operator VariantValue(Struct value) => value.AsVariantValue(); diff --git a/src/Tmds.DBus.Protocol/TypeModel.cs b/src/Tmds.DBus.Protocol/TypeModel.cs index dc639c06..011c00f8 100644 --- a/src/Tmds.DBus.Protocol/TypeModel.cs +++ b/src/Tmds.DBus.Protocol/TypeModel.cs @@ -54,10 +54,6 @@ public static DBusType GetTypeAlignment() { return DBusType.Signature; } - else if (typeof(T) == typeof(Variant)) - { - return DBusType.Variant; - } else if (typeof(T) == typeof(VariantValue)) { return DBusType.Variant; @@ -123,8 +119,6 @@ public static void EnsureSupportedVariantType() { } else if (typeof(T) == typeof(Signature)) { } - else if (typeof(T) == typeof(Variant)) - { } else if (typeof(T) == typeof(VariantValue)) { } else if (typeof(T).IsConstructedGenericType) @@ -187,8 +181,6 @@ private static void EnsureSupportedVariantType(Type type) { } else if (type == typeof(Signature)) { } - else if (type == typeof(Variant)) - { } else if (type == typeof(VariantValue)) { } else if (type.IsConstructedGenericType) @@ -296,11 +288,6 @@ private static int AppendTypeSignature(Type type, Span signature) signature[0] = (byte)DBusType.Signature; return 1; } - else if (type == typeof(Variant)) - { - signature[0] = (byte)DBusType.Variant; - return 1; - } else if (type == typeof(VariantValue)) { signature[0] = (byte)DBusType.Variant; diff --git a/src/Tmds.DBus.Protocol/Variant.cs b/src/Tmds.DBus.Protocol/Variant.cs deleted file mode 100644 index c9e87166..00000000 --- a/src/Tmds.DBus.Protocol/Variant.cs +++ /dev/null @@ -1,454 +0,0 @@ -namespace Tmds.DBus.Protocol; - -// This type is for writing so we don't need to add -// DynamicallyAccessedMemberTypes.PublicParameterlessConstructor. -#pragma warning disable IL2091 - -#if !DEBUG -[Obsolete($"{nameof(Variant)} will be removed. Use the {nameof(VariantValue)} type instead.")] -#endif -public readonly struct Variant -{ - private static readonly object Int64Type = DBusType.Int64; - private static readonly object UInt64Type = DBusType.UInt64; - private static readonly object DoubleType = DBusType.Double; - private readonly object? _o; - private readonly long _l; - - private const int TypeShift = 8 * 7; - private const int SignatureFirstShift = 8 * 6; - private const long StripTypeMask = ~(0xffL << TypeShift); - - private DBusType Type - => DetermineType(); - - public Variant(byte value) - { - _l = value | ((long)DBusType.Byte << TypeShift); - _o = null; - } - public Variant(bool value) - { - _l = (value ? 1L : 0) | ((long)DBusType.Bool << TypeShift); - _o = null; - } - public Variant(short value) - { - _l = (ushort)value | ((long)DBusType.Int16 << TypeShift); - _o = null; - } - public Variant(ushort value) - { - _l = value | ((long)DBusType.UInt16 << TypeShift); - _o = null; - } - public Variant(int value) - { - _l = (uint)value | ((long)DBusType.Int32 << TypeShift); - _o = null; - } - public Variant(uint value) - { - _l = value | ((long)DBusType.UInt32 << TypeShift); - _o = null; - } - public Variant(long value) - { - _l = value; - _o = Int64Type; - } - public Variant(ulong value) - { - _l = (long)value; - _o = UInt64Type; - } - public unsafe Variant(double value) - { - _l = *(long*)&value; - _o = DoubleType; - } - public Variant(string value) - { - _l = (long)DBusType.String << TypeShift; - _o = value ?? throw new ArgumentNullException(nameof(value)); - } - public Variant(ObjectPath value) - { - _l = (long)DBusType.ObjectPath << TypeShift; - string s = value.ToString(); - if (s.Length == 0) - { - throw new ArgumentException(nameof(value)); - } - _o = s; - } - public Variant(Signature value) - { - _l = (long)DBusType.Signature << TypeShift; - byte[] data = value.Data; - if (data.Length == 0) - { - throw new ArgumentException(nameof(value)); - } - _o = data; - } - public Variant(SafeHandle value) - { - _l = (long)DBusType.UnixFd << TypeShift; - _o = value ?? throw new ArgumentNullException(nameof(value)); - } - - public static implicit operator Variant(byte value) - => new Variant(value); - public static implicit operator Variant(bool value) - => new Variant(value); - public static implicit operator Variant(short value) - => new Variant(value); - public static implicit operator Variant(ushort value) - => new Variant(value); - public static implicit operator Variant(int value) - => new Variant(value); - public static implicit operator Variant(uint value) - => new Variant(value); - public static implicit operator Variant(long value) - => new Variant(value); - public static implicit operator Variant(ulong value) - => new Variant(value); - public static implicit operator Variant(double value) - => new Variant(value); - public static implicit operator Variant(string value) - => new Variant(value); - public static implicit operator Variant(ObjectPath value) - => new Variant(value); - public static implicit operator Variant(Signature value) - => new Variant(value); - public static implicit operator Variant(SafeHandle value) - => new Variant(value); - - public static Variant FromArray(Array value) where T : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromDict(Dict value) - where TKey : notnull - where TValue : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - where T3 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - where T3 : notnull - where T4 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - where T3 : notnull - where T4 : notnull - where T5 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - where T3 : notnull - where T4 : notnull - where T5 : notnull - where T6 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - where T3 : notnull - where T4 : notnull - where T5 : notnull - where T6 : notnull - where T7 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - where T3 : notnull - where T4 : notnull - where T5 : notnull - where T6 : notnull - where T7 : notnull - where T8 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - where T3 : notnull - where T4 : notnull - where T5 : notnull - where T6 : notnull - where T7 : notnull - where T8 : notnull - where T9 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - public static Variant FromStruct(Struct value) - where T1 : notnull - where T2 : notnull - where T3 : notnull - where T4 : notnull - where T5 : notnull - where T6 : notnull - where T7 : notnull - where T8 : notnull - where T9 : notnull - where T10 : notnull - { - Span buffer = stackalloc byte[ProtocolConstants.MaxSignatureLength]; - return new Variant(TypeModel.GetSignature>(buffer), value); - } - - // Dictionary, Struct, Array. - private unsafe Variant(ReadOnlySpan signature, IDBusWritable value) - { - if (value is null) - { - throw new ArgumentNullException(nameof(value)); - } - // Store the signature in the long if it is large enough. - if (signature.Length <= 8) - { - long l = 0; - Span span = new Span(&l, 8); - signature.CopyTo(span); - if (BitConverter.IsLittleEndian) - { - l = BinaryPrimitives.ReverseEndianness(l); - } - - _l = l; - _o = value; - } - else - { - _l = (long)signature[0] << TypeShift; - _o = new ValueTuple(signature.ToArray(), value); - } - } - - private byte GetByte() - { - DebugAssertTypeIs(DBusType.Byte); - return (byte)(_l & StripTypeMask); - } - private bool GetBool() - { - DebugAssertTypeIs(DBusType.Bool); - return (_l & StripTypeMask) != 0; - } - private short GetInt16() - { - DebugAssertTypeIs(DBusType.Int16); - return (short)(_l & StripTypeMask); - } - private ushort GetUInt16() - { - DebugAssertTypeIs(DBusType.UInt16); - return (ushort)(_l & StripTypeMask); - } - private int GetInt32() - { - DebugAssertTypeIs(DBusType.Int32); - return (int)(_l & StripTypeMask); - } - private uint GetUInt32() - { - DebugAssertTypeIs(DBusType.UInt32); - return (uint)(_l & StripTypeMask); - } - private long GetInt64() - { - DebugAssertTypeIs(DBusType.Int64); - return _l; - } - private ulong GetUInt64() - { - DebugAssertTypeIs(DBusType.UInt64); - return (ulong)(_l); - } - private unsafe double GetDouble() - { - DebugAssertTypeIs(DBusType.Double); - double value; - *(long*)&value = _l; - return value; - } - private string GetString() - { - DebugAssertTypeIs(DBusType.String); - return (_o as string)!; - } - private string GetObjectPath() - { - DebugAssertTypeIs(DBusType.ObjectPath); - return (_o as string)!; - } - private byte[] GetSignature() - { - DebugAssertTypeIs(DBusType.Signature); - return (_o as byte[])!; - } - private SafeHandle GetUnixFd() - { - DebugAssertTypeIs(DBusType.UnixFd); - return (_o as SafeHandle)!; - } - - private void DebugAssertTypeIs(DBusType expected) - { - Debug.Assert(Type == expected); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private DBusType DetermineType() - { - // For most types, we store the DBusType in the highest byte of the long. - // Except for some types, like Int64, for which we store the value allocation free - // in the long, and use the object field to store the type. - DBusType type = (DBusType)(_l >> TypeShift); - if (_o is not null) - { - if (_o.GetType() == typeof(DBusType)) - { - type = (DBusType)_o; - } - } - return type; - } - - internal unsafe void WriteTo(ref MessageWriter writer) - { - switch (Type) - { - case DBusType.Byte: - writer.WriteVariantByte(GetByte()); - break; - case DBusType.Bool: - writer.WriteVariantBool(GetBool()); - break; - case DBusType.Int16: - writer.WriteVariantInt16(GetInt16()); - break; - case DBusType.UInt16: - writer.WriteVariantUInt16(GetUInt16()); - break; - case DBusType.Int32: - writer.WriteVariantInt32(GetInt32()); - break; - case DBusType.UInt32: - writer.WriteVariantUInt32(GetUInt32()); - break; - case DBusType.Int64: - writer.WriteVariantInt64(GetInt64()); - break; - case DBusType.UInt64: - writer.WriteVariantUInt64(GetUInt64()); - break; - case DBusType.Double: - writer.WriteVariantDouble(GetDouble()); - break; - case DBusType.String: - writer.WriteVariantString(GetString()); - break; - case DBusType.ObjectPath: - writer.WriteVariantObjectPath(GetObjectPath()); - break; - case DBusType.Signature: - writer.WriteVariantSignature(GetSignature()); - break; - case DBusType.UnixFd: - writer.WriteVariantHandle(GetUnixFd()); - break; - - case DBusType.Array: - case DBusType.Struct: - ReadOnlySpan signature; - IDBusWritable writable; - if ((_l << 8) == 0) - { - // The signature is stored in the object. - var o = (ValueTuple)_o!; - signature = o.Item1; - writable = o.Item2; - } - else - { - // The signature is stored in _l. - long l = _l; - if (BitConverter.IsLittleEndian) - { - l = BinaryPrimitives.ReverseEndianness(l); - } - Span span = new Span(&l, 8); - int length = span.IndexOf((byte)0); - if (length == -1) - { - length = 8; - } - signature = span.Slice(0, length); - writable = (_o as IDBusWritable)!; - } - writer.WriteSignature(signature); - writable.WriteTo(ref writer); - break; - default: - throw new InvalidOperationException($"Cannot write Variant of type {Type}."); - } - } -} diff --git a/src/Tmds.DBus.Protocol/VariantExtensions.cs b/src/Tmds.DBus.Protocol/VariantExtensions.cs deleted file mode 100644 index da37491e..00000000 --- a/src/Tmds.DBus.Protocol/VariantExtensions.cs +++ /dev/null @@ -1,41 +0,0 @@ -namespace Tmds.DBus.Protocol; - -// This type is for writing so we don't need to add -// DynamicallyAccessedMemberTypes.PublicParameterlessConstructor. -#pragma warning disable IL2091 - -public static class VariantExtensions -{ - public static Variant AsVariant(this byte value) - => new Variant(value); - - public static Variant AsVariant(this bool value) - => new Variant(value); - - public static Variant AsVariant(this short value) - => new Variant(value); - - public static Variant AsVariant(this ushort value) - => new Variant(value); - - public static Variant AsVariant(this int value) - => new Variant(value); - - public static Variant AsVariant(this uint value) - => new Variant(value); - - public static Variant AsVariant(this long value) - => new Variant(value); - - public static Variant AsVariant(this ulong value) - => new Variant(value); - - public static Variant AsVariant(this double value) - => new Variant(value); - - public static Variant AsVariant(this string value) - => new Variant(value); - - public static Variant AsVariant(this SafeHandle value) - => new Variant(value); -} \ No newline at end of file diff --git a/src/Tmds.DBus.Protocol/VariantValue.cs b/src/Tmds.DBus.Protocol/VariantValue.cs index f5d2156f..66c193a6 100644 --- a/src/Tmds.DBus.Protocol/VariantValue.cs +++ b/src/Tmds.DBus.Protocol/VariantValue.cs @@ -433,11 +433,10 @@ private string UnsafeGetString() private ObjectPath UnsafeGetObjectPath() => new ObjectPath(UnsafeGetString()); - [Obsolete($"Call {nameof(GetObjectPathAsString)} instead. {nameof(GetObjectPath)} will be changed to return a {nameof(ObjectPath)} in a future version, which will cause a breaking change.")] - public string GetObjectPath() + public ObjectPath GetObjectPath() { EnsureTypeIs(VariantValueType.ObjectPath); - return UnsafeGetString(); + return UnsafeGetObjectPath(); } public string GetObjectPathAsString() diff --git a/test/Tmds.DBus.Protocol.Tests/ExceptionTests.cs b/test/Tmds.DBus.Protocol.Tests/ExceptionTests.cs index 1f226c89..5e85137f 100644 --- a/test/Tmds.DBus.Protocol.Tests/ExceptionTests.cs +++ b/test/Tmds.DBus.Protocol.Tests/ExceptionTests.cs @@ -2,8 +2,6 @@ using System.Threading.Tasks; using Xunit; -#pragma warning disable CS0618 // Using obsolete members - namespace Tmds.DBus.Protocol.Tests { public class ExceptionTests @@ -21,7 +19,7 @@ public async Task ObserverDisposed() new MatchRule(), (Message message, object? state) => "", (Exception? ex, string s, object? s1, object? s2) => { tcs.SetResult(ex!); - }); + }, ObserverFlags.EmitOnObserverDispose); disposable.Dispose(); @@ -43,7 +41,7 @@ public async Task ConnectionDisposed() new MatchRule(), (Message message, object? state) => "", (Exception? ex, string s, object? s1, object? s2) => { tcs.SetResult(ex!); - }); + }, ObserverFlags.EmitOnConnectionDispose); conn1.Dispose(); diff --git a/test/Tmds.DBus.Protocol.Tests/ReaderTests.cs b/test/Tmds.DBus.Protocol.Tests/ReaderTests.cs index 1ad14cd6..48bc8d49 100644 --- a/test/Tmds.DBus.Protocol.Tests/ReaderTests.cs +++ b/test/Tmds.DBus.Protocol.Tests/ReaderTests.cs @@ -4,8 +4,6 @@ using Microsoft.Win32.SafeHandles; using Xunit; -#pragma warning disable CS0618 // Using obsolete generic read methods. - namespace Tmds.DBus.Protocol.Tests; public class ReaderTests diff --git a/test/Tmds.DBus.Protocol.Tests/WriterTests.cs b/test/Tmds.DBus.Protocol.Tests/WriterTests.cs index 3f9b94bf..4b0530cb 100644 --- a/test/Tmds.DBus.Protocol.Tests/WriterTests.cs +++ b/test/Tmds.DBus.Protocol.Tests/WriterTests.cs @@ -7,8 +7,6 @@ namespace Tmds.DBus.Protocol.Tests; -#pragma warning disable CS0618 // Using obsolete generic write methods. - public class WriterTests { private delegate void WriteFunction(ref MessageWriter writer, T value); @@ -159,12 +157,6 @@ public void WriteHandle(int alignment, byte[] bigEndianData, byte[] littleEndian } } - [Theory, MemberData(nameof(WriteVariantAsVariantTestData))] - public void WriteVariantAsVariant(Variant value, byte[] bigEndianData, byte[] littleEndianData) - { - TestWrite(value, (ref MessageWriter writer, Variant value) => writer.WriteVariant(value), alignment: 0, bigEndianData, littleEndianData); - } - [Theory, MemberData(nameof(WriteIntrospectionXmlTestData))] public void WriteIntrospectionXml(string expected, ReadOnlyMemory[] interfaceXmls, string[] nodeNames) { @@ -208,60 +200,6 @@ public void WriteVariantValueAsVariant(VariantValue value, byte[] bigEndianData, public static IEnumerable WriteVariantValueAsVariantTestData => ReaderTests.ReadVariantValueTestData; - - public static IEnumerable WriteVariantAsVariantTestData - { - get - { - var myDictionary = new Dict - { - { 1, "one" }, - { 2, "two" } - }; - return new[] - { - new object[] {new Variant(true), new byte[] {1, 98, 0, 0, 0, 0, 0, 1}, - new byte[] {1, 98, 0, 0, 1, 0, 0, 0}}, - new object[] {new Variant((byte)5), new byte[] {1, 121, 0, 5}, - new byte[] {1, 121, 0, 5}}, - new object[] {new Variant((short)0x0102), new byte[] {1, 110, 0, 0, 1, 2}, - new byte[] {1, 110, 0, 0, 2, 1}}, - new object[] {new Variant(0x01020304), new byte[] {1, 105, 0, 0, 1, 2, 3, 4}, - new byte[] {1, 105, 0, 0, 4, 3, 2, 1}}, - new object[] {new Variant(0x0102030405060708), new byte[] {1, 120, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}, - new byte[] {1, 120, 0, 0, 0, 0, 0, 0, 8, 7, 6, 5, 4, 3, 2, 1}}, - new object[] {new Variant(1.0), new byte[] {1, 100, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0}, - new byte[] {1, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63}}, - new object[] {new Variant((ushort)0x0102), new byte[] {1, 113, 0, 0, 1, 2}, - new byte[] {1, 113, 0, 0, 2, 1}}, - new object[] {new Variant((uint)0x01020304), new byte[] {1, 117, 0, 0, 1, 2, 3, 4}, - new byte[] {1, 117, 0, 0, 4, 3, 2, 1}}, - new object[] {new Variant((ulong)0x0102030405060708), new byte[] {1, 116, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}, - new byte[] {1, 116, 0, 0, 0, 0, 0, 0, 8, 7, 6, 5, 4, 3, 2, 1}}, - new object[] {new Variant("hw"), new byte[] {1, 115, 0, 0, 0, 0, 0, 2, 104, 119, 0}, - new byte[] {1, 115, 0, 0, 2, 0, 0, 0, 104, 119, 0}}, - new object[] {new ObjectPath("/a/b").AsVariant(), new byte[] {1, 111, 0, 0, 0, 0, 0, 4, 47, 97, 47, 98, 0}, - new byte[] {1, 111, 0, 0, 4, 0, 0, 0, 47, 97, 47, 98, 0}}, - new object[] {new Signature("sis"u8).AsVariant(), new byte[] {1, 103, 0, 3, 115, 105, 115, 0}, - new byte[] {1, 103, 0, 3, 115, 105, 115, 0}}, - new object[] {new Array([1, 2]).AsVariant(), - new byte[] {2, 97, 120, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2}, - new byte[] {2, 97, 120, 0, 16, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0}}, - new object[] {Struct.Create(1L, "hw").AsVariant(), new byte[] {4, 40, 120, 115, 41, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 104, 119, 0}, - new byte[] {4, 40, 120, 115, 41, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 104, 119, 0}}, - new object[] {myDictionary.AsVariant(), - new byte[] {5, 97, 123, 121, 115, 125, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 3, 111, 110, 101, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 3, 116, 119, 111, 0}, - new byte[] {5, 97, 123, 121, 115, 125, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 3, 0, 0, 0, 111, 110, 101, 0, 0, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 116, 119, 111, 0}}, - new object[] {Struct.Create((byte)1, (byte)2, (byte)3, (byte)4, (byte)5, (byte)6, (byte)7, (byte)8).AsVariant(), - new byte[] {10, 40, 121, 121, 121, 121, 121, 121, 121, 121, 41, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}, - new byte[] {10, 40, 121, 121, 121, 121, 121, 121, 121, 121, 41, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8}}, - new object[] {Struct.Create((Variant)1).AsVariant(), new byte[] {3, 40, 118, 41, 0, 0, 0, 0, 1, 105, 0, 0, 0, 0, 0, 1}, - new byte[] {3, 40, 118, 41, 0, 0, 0, 0, 1, 105, 0, 0, 1, 0, 0, 0}}, - // (v) - }; - } - } - private void TestWrite(T value, WriteFunction writeFunction, int alignment, byte[] bigEndianData, byte[] littleEndianData) { var writer = CreateWriter();