-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
201c23b
commit 1e2f8fa
Showing
23 changed files
with
949 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Georg Jung. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Threading.Channels; | ||
|
||
namespace FastBertTokenizer; | ||
|
||
#if NETFRAMEWORK | ||
internal static class Backports | ||
{ | ||
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> kvp, out TKey key, out TValue value) | ||
{ | ||
key = kvp.Key; | ||
value = kvp.Value; | ||
} | ||
} | ||
|
||
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1402:File may only contain a single type", Justification = "Keep the backports together")] | ||
internal static class ChannelReaderExtensions | ||
{ | ||
public static IAsyncEnumerable<T> AsAsyncEnumerable<T>(this ChannelReader<T> reader) | ||
{ | ||
if (reader == null) | ||
{ | ||
throw new ArgumentNullException(nameof(reader)); | ||
} | ||
|
||
return new ChannelReaderAsyncEnumerable<T>(reader); | ||
} | ||
|
||
// This is probably rather naive and may have bugs but it is for testing only and seems sufficient at this point. | ||
private class ChannelReaderAsyncEnumerable<T> : IAsyncEnumerable<T> | ||
{ | ||
private readonly ChannelReader<T> _reader; | ||
|
||
public ChannelReaderAsyncEnumerable(ChannelReader<T> reader) | ||
{ | ||
_reader = reader; | ||
} | ||
|
||
public IAsyncEnumerator<T> GetAsyncEnumerator(CancellationToken cancellationToken = default) | ||
{ | ||
return new ChannelReaderAsyncEnumerator(_reader, cancellationToken); | ||
} | ||
|
||
private class ChannelReaderAsyncEnumerator : IAsyncEnumerator<T> | ||
{ | ||
private readonly ChannelReader<T> _reader; | ||
private readonly CancellationToken _cancellationToken; | ||
|
||
public ChannelReaderAsyncEnumerator(ChannelReader<T> reader, CancellationToken cancellationToken) | ||
{ | ||
_reader = reader; | ||
_cancellationToken = cancellationToken; | ||
Current = default!; | ||
} | ||
|
||
public T Current { get; private set; } | ||
|
||
public async ValueTask<bool> MoveNextAsync() | ||
{ | ||
if (_reader.Completion.IsCompleted) | ||
{ | ||
return false; | ||
} | ||
|
||
Current = await _reader.ReadAsync(); | ||
return true; | ||
} | ||
|
||
public ValueTask DisposeAsync() | ||
{ | ||
return default; | ||
} | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.