Skip to content

Commit

Permalink
Modified ImapStream and ImapEngine ReadTokenAsync methods to return V…
Browse files Browse the repository at this point in the history
…alueTask

Part of an ongoing series of fixes for issue #1335
  • Loading branch information
jstedfast committed Feb 22, 2022
1 parent 0a6cba6 commit a5b7817
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
6 changes: 3 additions & 3 deletions MailKit/MailKit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
<DefineConstants>$(DefineConstants);SERIALIZABLE</DefineConstants>
</PropertyGroup>

<!--ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) ">
<Reference Include="System.Net" />
</ItemGroup-->
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) Or '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\submodules\MimeKit\MimeKit\MimeKit.csproj" />
Expand Down
6 changes: 3 additions & 3 deletions MailKit/MailKitLite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
<DefineConstants>$(DefineConstants);SERIALIZABLE</DefineConstants>
</PropertyGroup>

<!--ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) ">
<Reference Include="System.Net" />
</ItemGroup-->
<ItemGroup Condition=" $(TargetFramework.StartsWith('net4')) Or '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\submodules\MimeKit\MimeKit\MimeKitLite.csproj" />
Expand Down
24 changes: 14 additions & 10 deletions MailKit/Net/Imap/ImapEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,23 +805,27 @@ public Task<string> ReadLineAsync (CancellationToken cancellationToken)
}
#endif

internal Task<ImapToken> ReadTokenAsync (string specials, bool doAsync, CancellationToken cancellationToken)
internal ValueTask<ImapToken> ReadTokenAsync (string specials, bool doAsync, CancellationToken cancellationToken)
{
if (doAsync)
return Stream.ReadTokenAsync (specials, cancellationToken);

return Task.FromResult (Stream.ReadToken (specials, cancellationToken));
var token = Stream.ReadToken (specials, cancellationToken);

return new ValueTask<ImapToken> (token);
}

internal Task<ImapToken> ReadTokenAsync (bool doAsync, CancellationToken cancellationToken)
internal ValueTask<ImapToken> ReadTokenAsync (bool doAsync, CancellationToken cancellationToken)
{
if (doAsync)
return Stream.ReadTokenAsync (ImapStream.DefaultSpecials, cancellationToken);

return Task.FromResult (Stream.ReadToken (ImapStream.DefaultSpecials, cancellationToken));
var token = Stream.ReadToken (ImapStream.DefaultSpecials, cancellationToken);

return new ValueTask<ImapToken> (token);
}

internal async Task<ImapToken> PeekTokenAsync (string specials, bool doAsync, CancellationToken cancellationToken)
internal async ValueTask<ImapToken> PeekTokenAsync (string specials, bool doAsync, CancellationToken cancellationToken)
{
ImapToken token;

Expand All @@ -835,7 +839,7 @@ internal async Task<ImapToken> PeekTokenAsync (string specials, bool doAsync, Ca
return token;
}

internal Task<ImapToken> PeekTokenAsync (bool doAsync, CancellationToken cancellationToken)
internal ValueTask<ImapToken> PeekTokenAsync (bool doAsync, CancellationToken cancellationToken)
{
return PeekTokenAsync (ImapStream.DefaultSpecials, doAsync, cancellationToken);
}
Expand Down Expand Up @@ -880,7 +884,7 @@ public ImapToken ReadToken (CancellationToken cancellationToken)
/// <exception cref="ImapProtocolException">
/// An IMAP protocol error occurred.
/// </exception>
public Task<ImapToken> ReadTokenAsync (CancellationToken cancellationToken)
public ValueTask<ImapToken> ReadTokenAsync (CancellationToken cancellationToken)
{
return Stream.ReadTokenAsync (cancellationToken);
}
Expand Down Expand Up @@ -926,7 +930,7 @@ public ImapToken PeekToken (string specials, CancellationToken cancellationToken
/// <exception cref="ImapProtocolException">
/// An IMAP protocol error occurred.
/// </exception>
public Task<ImapToken> PeekTokenAsync (string specials, CancellationToken cancellationToken)
public ValueTask<ImapToken> PeekTokenAsync (string specials, CancellationToken cancellationToken)
{
return PeekTokenAsync (specials, true, cancellationToken);
}
Expand Down Expand Up @@ -970,7 +974,7 @@ public ImapToken PeekToken (CancellationToken cancellationToken)
/// <exception cref="ImapProtocolException">
/// An IMAP protocol error occurred.
/// </exception>
public Task<ImapToken> PeekTokenAsync (CancellationToken cancellationToken)
public ValueTask<ImapToken> PeekTokenAsync (CancellationToken cancellationToken)
{
return PeekTokenAsync (true, cancellationToken);
}
Expand Down Expand Up @@ -1045,7 +1049,7 @@ public Task<string> ReadLiteralAsync (CancellationToken cancellationToken)
}
#endif

async Task SkipLineAsync (bool doAsync, CancellationToken cancellationToken)
async ValueTask SkipLineAsync (bool doAsync, CancellationToken cancellationToken)
{
ImapToken token;

Expand Down
18 changes: 9 additions & 9 deletions MailKit/Net/Imap/ImapStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ int ReadAhead (int atleast, CancellationToken cancellationToken)
return inputEnd - inputIndex;
}

async Task<int> ReadAheadAsync (int atleast, CancellationToken cancellationToken)
async ValueTask<int> ReadAheadAsync (int atleast, CancellationToken cancellationToken)
{
if (!AlignReadAheadBuffer (atleast, out int left, out int start, out int end))
return left;
Expand Down Expand Up @@ -591,7 +591,7 @@ ImapToken ReadQuotedStringToken (CancellationToken cancellationToken)
}
}

async Task<ImapToken> ReadQuotedStringTokenAsync (CancellationToken cancellationToken)
async ValueTask<ImapToken> ReadQuotedStringTokenAsync (CancellationToken cancellationToken)
{
bool escaped = false;

Expand Down Expand Up @@ -636,7 +636,7 @@ ImapToken ReadAtomString (ImapTokenType type, string specials, CancellationToken
}
}

async Task<ImapToken> ReadAtomStringAsync (ImapTokenType type, string specials, CancellationToken cancellationToken)
async ValueTask<ImapToken> ReadAtomStringAsync (ImapTokenType type, string specials, CancellationToken cancellationToken)
{
using (var builder = new ByteArrayBuilder (32)) {
if (type == ImapTokenType.Flag)
Expand All @@ -654,7 +654,7 @@ ImapToken ReadAtomToken (string specials, CancellationToken cancellationToken)
return ReadAtomString (ImapTokenType.Atom, specials, cancellationToken);
}

Task<ImapToken> ReadAtomTokenAsync (string specials, CancellationToken cancellationToken)
ValueTask<ImapToken> ReadAtomTokenAsync (string specials, CancellationToken cancellationToken)
{
return ReadAtomStringAsync (ImapTokenType.Atom, specials, cancellationToken);
}
Expand All @@ -666,7 +666,7 @@ ImapToken ReadFlagToken (string specials, CancellationToken cancellationToken)
return ReadAtomString (ImapTokenType.Flag, specials, cancellationToken);
}

Task<ImapToken> ReadFlagTokenAsync (string specials, CancellationToken cancellationToken)
ValueTask<ImapToken> ReadFlagTokenAsync (string specials, CancellationToken cancellationToken)
{
inputIndex++;

Expand Down Expand Up @@ -745,7 +745,7 @@ ImapToken ReadLiteralToken (CancellationToken cancellationToken)
}
}

async Task<ImapToken> ReadLiteralTokenAsync (CancellationToken cancellationToken)
async ValueTask<ImapToken> ReadLiteralTokenAsync (CancellationToken cancellationToken)
{
using (var builder = new ByteArrayBuilder (16)) {
// skip over the '{'
Expand Down Expand Up @@ -861,7 +861,7 @@ public ImapToken ReadToken (string specials, CancellationToken cancellationToken
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
public async Task<ImapToken> ReadTokenAsync (string specials, CancellationToken cancellationToken)
public async ValueTask<ImapToken> ReadTokenAsync (string specials, CancellationToken cancellationToken)
{
CheckDisposed ();

Expand Down Expand Up @@ -928,7 +928,7 @@ public ImapToken ReadToken (CancellationToken cancellationToken)
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
public Task<ImapToken> ReadTokenAsync (CancellationToken cancellationToken)
public ValueTask<ImapToken> ReadTokenAsync (CancellationToken cancellationToken)
{
return ReadTokenAsync (DefaultSpecials, cancellationToken);
}
Expand Down Expand Up @@ -1025,7 +1025,7 @@ internal bool ReadLine (ByteArrayBuilder builder, CancellationToken cancellation
/// <exception cref="System.IO.IOException">
/// An I/O error occurred.
/// </exception>
internal async Task<bool> ReadLineAsync (ByteArrayBuilder builder, CancellationToken cancellationToken)
internal async ValueTask<bool> ReadLineAsync (ByteArrayBuilder builder, CancellationToken cancellationToken)
{
CheckDisposed ();

Expand Down
6 changes: 3 additions & 3 deletions MailKit/Net/Imap/ImapUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ public static Task ParseMetadataAsync (ImapEngine engine, ImapCommand ic, int in
return ParseMetadataAsync (engine, metadata, doAsync, ic.CancellationToken);
}

internal static async Task<string> ReadStringTokenAsync (ImapEngine engine, string format, bool doAsync, CancellationToken cancellationToken)
internal static async ValueTask<string> ReadStringTokenAsync (ImapEngine engine, string format, bool doAsync, CancellationToken cancellationToken)
{
var token = await engine.ReadTokenAsync (doAsync, cancellationToken).ConfigureAwait (false);

Expand All @@ -746,7 +746,7 @@ internal static async Task<string> ReadStringTokenAsync (ImapEngine engine, stri
}
}

static async Task<string> ReadNStringTokenAsync (ImapEngine engine, string format, bool rfc2047, bool doAsync, CancellationToken cancellationToken)
static async ValueTask<string> ReadNStringTokenAsync (ImapEngine engine, string format, bool rfc2047, bool doAsync, CancellationToken cancellationToken)
{
var token = await engine.ReadTokenAsync (doAsync, cancellationToken).ConfigureAwait (false);
string value;
Expand Down Expand Up @@ -774,7 +774,7 @@ static async Task<string> ReadNStringTokenAsync (ImapEngine engine, string forma
return value;
}

static async Task<uint> ReadNumberAsync (ImapEngine engine, string format, bool doAsync, CancellationToken cancellationToken)
static async ValueTask<uint> ReadNumberAsync (ImapEngine engine, string format, bool doAsync, CancellationToken cancellationToken)
{
var token = await engine.ReadTokenAsync (doAsync, cancellationToken).ConfigureAwait (false);

Expand Down
5 changes: 5 additions & 0 deletions nuget/MailKit.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,19 @@ Features include:
</frameworkAssemblies>
<dependencies>
<group targetFramework="net452">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKit" version="3.1.1" />
</group>
<group targetFramework="net461">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKit" version="3.1.1" />
</group>
<group targetFramework="net47">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKit" version="3.1.1" />
</group>
<group targetFramework="net48">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKit" version="3.1.1" />
</group>
<group targetFramework="net5.0">
Expand All @@ -79,6 +83,7 @@ Features include:
<dependency id="MimeKit" version="3.1.1" />
</group>
<group targetFramework="netstandard2.0">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKit" version="3.1.1" />
</group>
<group targetFramework="netstandard2.1">
Expand Down
5 changes: 5 additions & 0 deletions nuget/MailKitLite.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,19 @@ Features include:
</frameworkAssemblies>
<dependencies>
<group targetFramework="net452">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKitLite" version="3.1.1" />
</group>
<group targetFramework="net461">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKitLite" version="3.1.1" />
</group>
<group targetFramework="net47">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKitLite" version="3.1.1" />
</group>
<group targetFramework="net48">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKitLite" version="3.1.1" />
</group>
<group targetFramework="net5.0">
Expand All @@ -79,6 +83,7 @@ Features include:
<dependency id="MimeKitLite" version="3.1.1" />
</group>
<group targetFramework="netstandard2.0">
<dependency id="System.Threading.Tasks.Extensions" version="4.5.4" />
<dependency id="MimeKitLite" version="3.1.1" />
</group>
<group targetFramework="netstandard2.1">
Expand Down

0 comments on commit a5b7817

Please sign in to comment.