Ejemplo n.º 1
0
        ValueTask IAsyncBinaryWriter.WriteAsync(ReadOnlyMemory <byte> input, LengthFormat?lengthFormat, CancellationToken token)
        {
            Task result;

            if (token.IsCancellationRequested)
            {
                result = Task.FromCanceled(token);
            }
            else
            {
                result = Task.CompletedTask;
                try
                {
                    if (lengthFormat.HasValue)
                    {
                        writer.WriteLength(input.Length, lengthFormat.GetValueOrDefault());
                    }

                    writer.Write(input.Span);
                }
                catch (Exception e)
                {
                    result = Task.FromException(e);
                }
            }

            return(new ValueTask(result));
        }
Ejemplo n.º 2
0
        ValueTask IAsyncBinaryWriter.WriteAsync(ReadOnlyMemory <byte> input, LengthFormat?lengthFormat, CancellationToken token)
        {
            ValueTask result;

            if (token.IsCancellationRequested)
            {
#if NETSTANDARD2_1
                result = new (Task.FromCanceled(token));
#else
                result = ValueTask.FromCanceled(token);
#endif
            }
            else
            {
                result = new();
                try
                {
                    if (lengthFormat.HasValue)
                    {
                        writer.WriteLength(input.Length, lengthFormat.GetValueOrDefault());
                    }

                    writer.Write(input.Span);
                }
                catch (Exception e)
                {
#if NETSTANDARD2_1
                    result = new (Task.FromException(e));
#else
                    result = ValueTask.FromException(e);
#endif
                }
            }

            return(result);
        }