< Summary

Information
Class: IceRpc.Ice.Codec.IceEncoderExtensions
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Ice/Codec/IceEncoderExtensions.cs
Tag: 1321_24790053727
Line coverage
68%
Covered lines: 44
Uncovered lines: 20
Coverable lines: 64
Total lines: 129
Line coverage: 68.7%
Branch coverage
69%
Covered branches: 18
Total branches: 26
Branch coverage: 69.2%
Method coverage
80%
Covered methods: 4
Fully covered methods: 3
Total methods: 5
Method coverage: 80%
Full method coverage: 60%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
EncodeDictionary(...)75%5464.28%
EncodeSequence(...)90%101086.66%
EncodeSequence(...)100%44100%
EncodeSpan(...)100%22100%
WriteByteSequence(...)0%4260%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Ice/Codec/IceEncoderExtensions.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Buffers;
 4using System.Collections.Immutable;
 5using System.Runtime.InteropServices;
 6
 7namespace IceRpc.Ice.Codec;
 8
 9/// <summary>Provides extension methods for <see cref="IceEncoder" /> to encode sequences or dictionaries.</summary>
 10public static class IceEncoderExtensions
 11{
 12    /// <summary>Encodes a dictionary.</summary>
 13    /// <typeparam name="TKey">The dictionary key type.</typeparam>
 14    /// <typeparam name="TValue">The dictionary value type.</typeparam>
 15    /// <param name="encoder">The Ice encoder.</param>
 16    /// <param name="v">The dictionary to encode.</param>
 17    /// <param name="keyEncodeAction">The encode action for the keys.</param>
 18    /// <param name="valueEncodeAction">The encode action for the values.</param>
 19    public static void EncodeDictionary<TKey, TValue>(
 20        this ref IceEncoder encoder,
 21        IEnumerable<KeyValuePair<TKey, TValue>> v,
 22        EncodeAction<TKey> keyEncodeAction,
 23        EncodeAction<TValue> valueEncodeAction)
 24        where TKey : notnull
 3225    {
 3226        if (!v.TryGetNonEnumeratedCount(out int count))
 027        {
 028            KeyValuePair<TKey, TValue>[] array = v.ToArray();
 029            count = array.Length;
 030            v = array;
 031        }
 3232        encoder.EncodeSize(count);
 290233        foreach ((TKey key, TValue value) in v)
 140334        {
 140335            keyEncodeAction(ref encoder, key);
 140336            valueEncodeAction(ref encoder, value);
 140337        }
 3238    }
 39
 40    /// <summary>Encodes a sequence of fixed-size numeric values, such as int or ulong.</summary>
 41    /// <typeparam name="T">The sequence element type.</typeparam>
 42    /// <param name="encoder">The Ice encoder.</param>
 43    /// <param name="v">The sequence of numeric values.</param>
 44    public static void EncodeSequence<T>(this ref IceEncoder encoder, IEnumerable<T> v)
 45        where T : struct
 2546    {
 2547        switch (v)
 48        {
 49            case T[] vArray:
 1950                encoder.EncodeSpan(new ReadOnlySpan<T>(vArray));
 1951                break;
 52
 53            case ImmutableArray<T> vImmutableArray:
 254                encoder.EncodeSpan(vImmutableArray.AsSpan());
 255                break;
 56
 57            case ArraySegment<T> vArraySegment:
 258                encoder.EncodeSpan((ReadOnlySpan<T>)vArraySegment.AsSpan());
 259                break;
 60
 61            case List<T> list:
 162                encoder.EncodeSpan((ReadOnlySpan<T>)CollectionsMarshal.AsSpan(list));
 163                break;
 64
 65            default:
 166                encoder.EncodeSequence(
 167                    v,
 25768                    (ref IceEncoder encoder, T element) => encoder.EncodeFixedSizeNumeric(element));
 169                break;
 70        }
 2571    }
 72
 73    /// <summary>Encodes a sequence.</summary>
 74    /// <typeparam name="T">The type of the sequence elements. It is non-nullable except for class and proxy types.
 75    /// </typeparam>
 76    /// <param name="encoder">The Ice encoder.</param>
 77    /// <param name="v">The sequence to encode.</param>
 78    /// <param name="encodeAction">The encode action for an element.</param>
 79    public static void EncodeSequence<T>(this ref IceEncoder encoder, IEnumerable<T> v, EncodeAction<T> encodeAction)
 147480    {
 147481        if (!v.TryGetNonEnumeratedCount(out int count))
 782        {
 783            var array = v.ToArray();
 784            count = array.Length;
 785            v = array;
 786        }
 147487        encoder.EncodeSize(count);
 992088        foreach (T item in v)
 274989        {
 274990            encodeAction(ref encoder, item);
 274991        }
 147492    }
 93
 94    /// <summary>Encodes a span of fixed-size numeric values, such as int or ulong.</summary>
 95    /// <typeparam name="T">The span element type.</typeparam>
 96    /// <param name="encoder">The Ice encoder.</param>
 97    /// <param name="v">The span of numeric values represented by a <see cref="ReadOnlySpan{T}" />.</param>
 98    public static void EncodeSpan<T>(this ref IceEncoder encoder, ReadOnlySpan<T> v)
 99        where T : struct
 33100    {
 101        // This method works because (as long as) there is no padding in the memory representation of the ReadOnlySpan.
 33102        encoder.EncodeSize(v.Length);
 33103        if (!v.IsEmpty)
 29104        {
 29105            encoder.WriteByteSpan(MemoryMarshal.AsBytes(v));
 29106        }
 33107    }
 108
 109    /// <summary>Copies a sequence of bytes to the underlying buffer writer.</summary>
 110    /// <param name="encoder">The Ice encoder.</param>
 111    /// <param name="v">The sequence to copy.</param>
 112    public static void WriteByteSequence(this ref IceEncoder encoder, ReadOnlySequence<byte> v)
 0113    {
 0114        if (!v.IsEmpty)
 0115        {
 0116            if (v.IsSingleSegment)
 0117            {
 0118                encoder.WriteByteSpan(v.FirstSpan);
 0119            }
 120            else
 0121            {
 0122                foreach (ReadOnlyMemory<byte> buffer in v)
 0123                {
 0124                    encoder.WriteByteSpan(buffer.Span);
 0125                }
 0126            }
 0127        }
 0128    }
 129}