Skip to content

Commit 0fcf702

Browse files
authored
Merge pull request #1 from roji/handler-work-stuff
Some partial style changes mostly
2 parents 246adb4 + cbe268f commit 0fcf702

24 files changed

Lines changed: 181 additions & 181 deletions

src/Npgsql/BackendMessages/RowDescriptionMessage.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ internal PgConverterInfo GetOrAddConverterInfo(Type type)
366366
PgConverterInfo info;
367367
if (ObjectOrDefaultTypeInfo.Type == type)
368368
info = ObjectOrDefaultInfo;
369-
else if (_lastTypeInfo is { } lastTypeInfo && lastTypeInfo.Type == type)
369+
else if (_lastTypeInfo?.Type == type)
370370
info = _lastInfo;
371371
else
372372
{

src/Npgsql/Internal/AdoTypeInfoResolver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ static void AddInfos(TypeInfoMappingCollection mappings)
9292

9393
// Float8
9494
mappings.AddStructType<double>(DataTypeNames.Float8,
95-
static (options, mapping, _) => mapping.CreateInfo(options, new DoubleConverter<double>()), isDefault: true);
95+
static (options, mapping, _) => mapping.CreateInfo(options, new DoubleConverter()), isDefault: true);
9696

9797
// Numeric
9898
mappings.AddStructType<BigInteger>(DataTypeNames.Numeric,
Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,15 @@
1-
using System;
2-
using System.Numerics;
3-
41
namespace Npgsql.Internal.Converters;
52

6-
sealed class DoubleConverter<T> : PgBufferedConverter<T>
7-
#if NET7_0_OR_GREATER
8-
where T : INumberBase<T>
9-
#endif
3+
sealed class DoubleConverter : PgBufferedConverter<double>
104
{
115
public override bool CanConvert(DataFormat format, out BufferingRequirement bufferingRequirement)
126
{
137
bufferingRequirement = BufferingRequirement.FixedSize;
148
return base.CanConvert(format, out _);
159
}
16-
public override Size GetSize(SizeContext context, T value, ref object? writeState) => sizeof(double);
17-
18-
#if NET7_0_OR_GREATER
19-
protected override T ReadCore(PgReader reader) => T.CreateChecked(reader.ReadDouble());
20-
protected override void WriteCore(PgWriter writer, T value) => writer.WriteDouble(double.CreateChecked(value));
21-
#else
22-
protected override T ReadCore(PgReader reader)
23-
{
24-
var value = reader.ReadDouble();
25-
if (typeof(double) == typeof(T))
26-
return (T)(object)(double)value;
2710

28-
throw new NotSupportedException();
29-
}
11+
public override Size GetSize(SizeContext context, double value, ref object? writeState) => sizeof(double);
3012

31-
protected override void WriteCore(PgWriter writer, T value)
32-
{
33-
if (typeof(float) == typeof(T))
34-
writer.WriteDouble((double)(object)value!);
35-
else
36-
throw new NotSupportedException();
37-
}
38-
#endif
13+
protected override double ReadCore(PgReader reader) => reader.ReadDouble();
14+
protected override void WriteCore(PgWriter writer, double value) => writer.WriteDouble(value);
3915
}

src/Npgsql/Internal/Converters/Primitive/Int4Converter.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public override bool CanConvert(DataFormat format, out BufferingRequirement buff
1313
bufferingRequirement = BufferingRequirement.FixedSize;
1414
return base.CanConvert(format, out _);
1515
}
16+
1617
public override Size GetSize(SizeContext context, T value, ref object? writeState) => sizeof(int);
1718

1819
#if NET7_0_OR_GREATER

src/Npgsql/Internal/Converters/Primitive/Int8Converter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public override bool CanConvert(DataFormat format, out BufferingRequirement buff
2222
protected override T ReadCore(PgReader reader)
2323
{
2424
var value = reader.ReadInt64();
25+
if (typeof(long) == typeof(T))
26+
return (T)(object)value;
27+
2528
if (typeof(short) == typeof(T))
2629
return (T)(object)checked((short)value);
2730
if (typeof(int) == typeof(T))
2831
return (T)(object)checked((int)value);
29-
if (typeof(long) == typeof(T))
30-
return (T)(object)value;
3132

3233
if (typeof(byte) == typeof(T))
3334
return (T)(object)checked((byte)value);

src/Npgsql/Internal/Converters/Primitive/TextConverters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ protected override void WriteCore(PgWriter writer, char value)
183183
// Move these out for code size/sharing.
184184
static class TextConverter
185185
{
186-
public static Size GetSize(ref SizeContext context, ReadOnlyMemory<char> value, Encoding encoding) =>
187-
encoding.GetByteCount(value.Span);
186+
public static Size GetSize(ref SizeContext context, ReadOnlyMemory<char> value, Encoding encoding)
187+
=> encoding.GetByteCount(value.Span);
188188

189189
// Adapted version of GetString(ROSeq) removing the intermediate string allocation to make a contiguous char array.
190190
public static char[] GetChars(Encoding encoding, ReadOnlySequence<byte> bytes)

src/Npgsql/Internal/Converters/Temporal/DateTimeConverterResolvers.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,23 @@ public override PgConverterResolution Get(DateTime value, PgTypeId? expectedPgTy
3333
if (value.Kind is DateTimeKind.Utc)
3434
{
3535
if (expectedPgTypeId == _timestamp)
36+
{
3637
throw new ArgumentException(
3738
"Cannot write DateTime with Kind=UTC to PostgreSQL type 'timestamp without time zone', " +
3839
"consider using 'timestamp with time zone'. " +
3940
"Note that it's not possible to mix DateTimes with different Kinds in an array/range.", nameof(value));
41+
}
4042

4143
// We coalesce with expectedPgTypeId to throw on unknown type ids.
4244
return GetDefault(expectedPgTypeId ?? _timestampTz);
4345
}
4446

4547
if (expectedPgTypeId == _timestampTz)
48+
{
4649
throw new ArgumentException(
4750
$"Cannot write DateTime with Kind={value.Kind} to PostgreSQL type 'timestamp with time zone', only UTC is supported. " +
4851
"Note that it's not possible to mix DateTimes with different Kinds in an array/range. ", nameof(value));
52+
}
4953

5054
// We coalesce with expectedPgTypeId to throw on unknown type ids.
5155
return GetDefault(expectedPgTypeId ?? _timestamp);

src/Npgsql/Internal/NpgsqlDatabaseInfo.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -339,18 +339,12 @@ internal static void ResetFactories()
339339
#endregion Factory management
340340

341341
internal Oid GetOid(PgTypeId pgTypeId, bool validate = false)
342-
{
343-
if (pgTypeId.IsOid)
344-
return validate ? GetPostgresTypeByOid(pgTypeId.Oid).OID : pgTypeId.Oid;
345-
346-
return GetPostgresTypeByName(pgTypeId.DataTypeName).OID;
347-
}
342+
=> pgTypeId.IsOid
343+
? validate ? GetPostgresTypeByOid(pgTypeId.Oid).OID : pgTypeId.Oid
344+
: GetPostgresTypeByName(pgTypeId.DataTypeName).OID;
348345

349346
internal DataTypeName GetDataTypeName(PgTypeId pgTypeId, bool validate = false)
350-
{
351-
if (pgTypeId.IsDataTypeName)
352-
return validate ? GetPostgresTypeByName(pgTypeId.DataTypeName).DataTypeName : pgTypeId.DataTypeName;
353-
354-
return GetPostgresTypeByOid(pgTypeId.Oid).DataTypeName;
355-
}
347+
=> pgTypeId.IsDataTypeName
348+
? validate ? GetPostgresTypeByName(pgTypeId.DataTypeName).DataTypeName : pgTypeId.DataTypeName
349+
: GetPostgresTypeByOid(pgTypeId.Oid).DataTypeName;
356350
}

src/Npgsql/Internal/PgConverter.cs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ public virtual bool CanConvert(DataFormat format, out BufferingRequirement buffe
6767
}
6868

6969
/// When <see cref="CanConvert"/> returns BufferingRequirement.Custom this method can be called to determine the buffer requirements.
70-
public virtual void GetBufferRequirements(DataFormat format, out Size readRequirement, out Size writeRequirement) => throw new NotImplementedException();
70+
public virtual void GetBufferRequirements(DataFormat format, out Size readRequirement, out Size writeRequirement)
71+
=> throw new NotImplementedException();
7172

7273
internal abstract Type TypeToConvert { get; }
7374

@@ -82,35 +83,37 @@ internal bool IsDbNullValueAsObject([NotNullWhen(false)] object? value)
8283
};
8384

8485
// We do the null check to keep the NotNullWhen(false) invariant.
85-
bool Custom() => IsDbNullAsObject(value) || (value is null ? throw new ArgumentNullException("Null value given for non-nullable type converter") : false);
86+
bool Custom() => IsDbNullAsObject(value) || (value is null
87+
? throw new ArgumentNullException("Null value given for non-nullable type converter")
88+
: false);
8689
}
8790

8891
private protected abstract bool IsDbNullAsObject(object? value);
8992

9093
internal abstract Size GetSizeAsObject(SizeContext context, object value, ref object? writeState);
9194

92-
internal object ReadAsObject(PgReader reader) => ReadAsObject(async: false, reader, CancellationToken.None).GetAwaiter().GetResult();
93-
internal ValueTask<object> ReadAsObjectAsync(PgReader reader, CancellationToken cancellationToken = default) => ReadAsObject(async: true, reader, cancellationToken);
95+
internal object ReadAsObject(PgReader reader)
96+
=> ReadAsObject(async: false, reader, CancellationToken.None).GetAwaiter().GetResult();
97+
internal ValueTask<object> ReadAsObjectAsync(PgReader reader, CancellationToken cancellationToken = default)
98+
=> ReadAsObject(async: true, reader, cancellationToken);
9499

95100
// Shared sync/async abstract to reduce virtual method table size overhead and code size for each NpgsqlConverter<T> instantiation.
96101
private protected abstract ValueTask<object> ReadAsObject(bool async, PgReader reader, CancellationToken cancellationToken);
97102

98-
internal void WriteAsObject(PgWriter writer, object value) => WriteAsObject(async: false, writer, value, CancellationToken.None).GetAwaiter().GetResult();
99-
internal ValueTask WriteAsObjectAsync(PgWriter writer, object value, CancellationToken cancellationToken = default) => WriteAsObject(async: true, writer, value, cancellationToken);
103+
internal void WriteAsObject(PgWriter writer, object value)
104+
=> WriteAsObject(async: false, writer, value, CancellationToken.None).GetAwaiter().GetResult();
105+
internal ValueTask WriteAsObjectAsync(PgWriter writer, object value, CancellationToken cancellationToken = default)
106+
=> WriteAsObject(async: true, writer, value, cancellationToken);
100107

101108
// Shared sync/async abstract to reduce virtual method table size overhead and code size for each NpgsqlConverter<T> instantiation.
102109
private protected abstract ValueTask WriteAsObject(bool async, PgWriter writer, object value, CancellationToken cancellationToken);
103110

104111
static DbNullPredicate InferDbNullPredicate(Type type, bool isNullDefaultValue)
105-
{
106-
if (type == typeof(object) || type == typeof(DBNull))
107-
return DbNullPredicate.PolymorphicNull;
108-
109-
if (isNullDefaultValue)
110-
return DbNullPredicate.Null;
111-
112-
return DbNullPredicate.None;
113-
}
112+
=> type == typeof(object) || type == typeof(DBNull)
113+
? DbNullPredicate.PolymorphicNull
114+
: isNullDefaultValue
115+
? DbNullPredicate.Null
116+
: DbNullPredicate.None;
114117

115118
internal enum DbNullPredicate : byte
116119
{
@@ -125,7 +128,8 @@ internal enum DbNullPredicate : byte
125128
}
126129

127130
[DoesNotReturn]
128-
private protected static void ThrowIORequired() => throw new InvalidOperationException("Buffer requirements for current data format were not respected, expected no IO to be required.");
131+
private protected static void ThrowIORequired()
132+
=> throw new InvalidOperationException("Fixed sizedness for format not respected, expected no IO to be required.");
129133
}
130134

131135
public abstract class PgConverter<T> : PgConverter
@@ -150,7 +154,9 @@ public bool IsDbNullValue([NotNullWhen(false)] T? value)
150154
};
151155

152156
// We do the null check to keep the NotNullWhen(false) invariant.
153-
bool Custom() => IsDbNull(value) || (value is null ? throw new ArgumentNullException("Null value given for non-nullable type converter") : false);
157+
bool Custom() => IsDbNull(value) || (value is null
158+
? throw new ArgumentNullException("Null value given for non-nullable type converter")
159+
: false);
154160
}
155161

156162
public abstract T Read(PgReader reader);
@@ -167,14 +173,17 @@ internal sealed override Size GetSizeAsObject(SizeContext context, object value,
167173
=> GetSize(context, ReferenceEquals(value, null) ? default! : (T)value, ref writeState);
168174
}
169175

170-
// Using a function pointer here is safe against assembly unloading as the instance reference that the static pointer method lives on is passed along.
171-
// As such the instance cannot be collected by the gc which means the entire assembly is prevented from unloading until we're done.
176+
// Using a function pointer here is safe against assembly unloading as the instance reference that the static pointer method lives on is
177+
// passed along. As such the instance cannot be collected by the gc which means the entire assembly is prevented from unloading until we're
178+
// done.
172179
// The alternatives are:
173180
// 1. Add a virtual method and make AwaitTask call into it (bloating the vtable of all derived types).
174-
// 2. Using a delegate, meaning we add a static field + an alloc per T + metadata, slightly slower dispatch perf so overall strictly worse as well.
181+
// 2. Using a delegate, meaning we add a static field + an alloc per T + metadata, slightly slower dispatch perf so overall strictly worse
182+
// as well.
175183
static class PgStreamingConverterHelpers
176184
{
177-
// Split out from the generic class to amortize the huge size penalty per async state machine, which would otherwise be per instantiation.
185+
// Split out from the generic class to amortize the huge size penalty per async state machine, which would otherwise be per
186+
// instantiation.
178187
#if !NETSTANDARD
179188
[AsyncMethodBuilder(typeof(PoolingAsyncValueTaskMethodBuilder<>))]
180189
#endif
@@ -187,7 +196,8 @@ public static async ValueTask<object> AwaitTask(Task task, Continuation continua
187196
return result;
188197
}
189198

190-
// Split out into a struct as unsafe and async don't mix, while we do want a nicely typed function pointer signature to prevent mistakes.
199+
// Split out into a struct as unsafe and async don't mix, while we do want a nicely typed function pointer signature to prevent
200+
// mistakes.
191201
public readonly unsafe struct Continuation
192202
{
193203
public object Handle { get; }
@@ -209,16 +219,16 @@ public abstract class PgStreamingConverter<T> : PgConverter<T>
209219
{
210220
protected PgStreamingConverter(bool customDbNullPredicate = false) : base(customDbNullPredicate) { }
211221

212-
private protected sealed override unsafe ValueTask<object> ReadAsObject(bool async, PgReader reader, CancellationToken cancellationToken = default)
222+
private protected sealed override unsafe ValueTask<object> ReadAsObject(
223+
bool async, PgReader reader, CancellationToken cancellationToken = default)
213224
{
214225
if (!async)
215226
return new(Read(reader)!);
216227

217228
var task = ReadAsync(reader, cancellationToken);
218-
if (task.IsCompletedSuccessfully)
219-
return new(task.Result!);
220-
221-
return PgStreamingConverterHelpers.AwaitTask(task.AsTask(), new(this, &BoxResult));
229+
return task.IsCompletedSuccessfully
230+
? new(task.Result!)
231+
: PgStreamingConverterHelpers.AwaitTask(task.AsTask(), new(this, &BoxResult));
222232

223233
static object BoxResult(Task task)
224234
{

src/Npgsql/Internal/PgConverterResolver.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ public abstract class PgConverterResolver<T> : PgConverterResolver
9494
/// <param name="expectedPgTypeId"></param>
9595
/// <returns>The converter resolution.</returns>
9696
/// <remarks>
97-
/// Implementations should not return new instances of the possible converters that can be returned, instead its expected these are cached once used.
98-
/// Array or other collection converters depend on this to cache their own converter - which wraps the element converter - with the cache key being the element converter reference.
97+
/// Implementations should not return new instances of the possible converters that can be returned, instead its expected these are
98+
/// cached once used. Array or other collection converters depend on this to cache their own converter - which wraps the element
99+
/// converter - with the cache key being the element converter reference.
99100
/// </remarks>
100101
public abstract PgConverterResolution Get(T? value, PgTypeId? expectedPgTypeId);
101102

0 commit comments

Comments
 (0)