コード例 #1
0
    /// <summary>
    /// Tries to asynchronously receives a value from a specified source.
    /// </summary>
    /// <typeparam name="TOutput">The type of data contained in the source to receive.</typeparam>
    /// <param name="source">The source from which to receive the value.</param>
    /// <param name="timeout">
    /// The maximum time interval, in milliseconds, to wait for the synchronous operation to complete,
    /// or an interval that represents -1 milliseconds to wait indefinitely.
    /// </param>
    /// <returns>
    /// A task that represents the asynchronous receive operation. When an item value is successfully received from the source,
    /// the returned task is completed and its <see cref="Task{T}.Result"/> returns a successful <see cref="TryValue{TOutput}"/>
    /// containing the received value. If an item value cannot be retrieved because the source is empty and completed, the returned
    /// task is completed and its <see cref="Task{T}.Result"/> returns <see cref="TryValue.None()"/>.
    /// </returns>
    public static async Task <TryValue <TOutput> > TryReceiveAsync <TOutput>(
        this ISourceBlock <TOutput> source, TimeSpan timeout)
    {
        Contracts.Requires.That(source != null);
        Contracts.Requires.That(timeout.IsDuration());

        try
        {
            return(TryValue.New(await source.ReceiveAsync(timeout).DontMarshallContext()));
        }
        catch (InvalidOperationException)
        {
            return(TryValue.None <TOutput>());
        }
    }
コード例 #2
0
    public static TryValue <T> TryDeserialize <T>(this IValueKey <T> key, string value)
    {
        Contracts.Requires.That(key != null);

        T result;

        if (key.TryDeserialize(value, out result))
        {
            return(TryValue.New(result));
        }
        else
        {
            return(TryValue.None <T>());
        }
    }
コード例 #3
0
    public static async Task <TryValue <ReleaseStruct> > TryWaitForUsingBlockAsync(
        this SemaphoreSlim semaphore,
        TimeSpan timeout,
        CancellationToken cancellation = default(CancellationToken))
    {
        Contracts.Requires.That(semaphore != null);
        Contracts.Requires.That(Time.IsDuration(timeout));

        if (await semaphore.WaitAsync(timeout, cancellation).DontMarshallContext())
        {
            return(TryValue.New(new ReleaseStruct(semaphore)));
        }
        else
        {
            return(TryValue.None <ReleaseStruct>());
        }
    }
        /// <inheritdoc />
        public TValue this[TIndex index]
        {
            // this method's contracts will check that the dictionary contains the key first
            get
            {
                IDictionaryContracts.IndexerGet(this, index);
                IReadOnlyIndexableContracts.IndexerGet(this, index);

                return(this.indexable[index].Value);
            }

            set
            {
                IDictionaryContracts.IndexerSet(this, index);
                IIndexableContracts.IndexerSet(this, index);

                if (!this.indexable[index].HasValue)
                {
                    this.count++;
                }

                this.indexable[index] = TryValue.New(value);
            }
        }
コード例 #5
0
    public static TryValue <T> SingleOrNone <T>(this IEnumerable <T> values)
    {
        Contracts.Requires.That(values != null);

        return(values.IsEmpty() ? TryValue.None <T>() : TryValue.New(values.Single()));
    }
コード例 #6
0
    public static TryValue <T> ElementAtOrNone <T>(this IEnumerable <T> values, int index)
    {
        Contracts.Requires.That(values != null);
        Contracts.Requires.That(index >= 0);

        var readOnlyList = values as IReadOnlyList <T>;

        if (readOnlyList != null)
        {
            return(index.IsIn(readOnlyList.GetIndexRange()) ? TryValue.New(readOnlyList[index]) : TryValue.None <T>());
        }

        var list = values as IList <T>;

        if (list != null)
        {
            return(index.IsIn(list.GetListIndexRange()) ? TryValue.New(list[index]) : TryValue.None <T>());
        }

        int count = 0;

        foreach (var value in values)
        {
            if (count == index)
            {
                return(TryValue.New(value));
            }

            count++;
        }

        return(TryValue.None <T>());
    }