Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Added System.Memory.Polyfill Project #2339

Merged
merged 14 commits into from
Jun 5, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
c8d31c4
Bug fix in BufferWriter
KrzysztofCwalina Mar 20, 2018
8ee6979
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina Mar 22, 2018
33da66f
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina Mar 22, 2018
ee23d6b
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina Mar 22, 2018
132efef
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina Mar 23, 2018
f6d44ca
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina Apr 10, 2018
954f079
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina May 1, 2018
3c3b978
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina May 3, 2018
6b160a7
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina May 3, 2018
a5dfae9
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina May 10, 2018
b89ba55
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina May 31, 2018
23415be
Merge branch 'master' of https://github.com/dotnet/corefxlab
KrzysztofCwalina Jun 4, 2018
8a5e662
Added System.Memory.Polyfill Project
KrzysztofCwalina Jun 4, 2018
42e41c5
PR review feedback and build fix
KrzysztofCwalina Jun 5, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions corefxlab.sln
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Buffers.ReaderWriter
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Buffers.ReaderWriter.Tests", "tests\System.Buffers.ReaderWriter.Tests\System.Buffers.ReaderWriter.Tests.csproj", "{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Memory.Polyfill", "src\System.Memory.Polyfill\System.Memory.Polyfill.csproj", "{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Memory.Polyfill.Tests", "tests\System.Memory.Polyfill.Tests\System.Memory.Polyfill.Tests.csproj", "{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -556,6 +560,30 @@ Global
{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C}.Release|x64.Build.0 = Release|Any CPU
{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C}.Release|x86.ActiveCfg = Release|Any CPU
{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C}.Release|x86.Build.0 = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|x64.ActiveCfg = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|x64.Build.0 = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|x86.ActiveCfg = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Debug|x86.Build.0 = Debug|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|Any CPU.Build.0 = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|x64.ActiveCfg = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|x64.Build.0 = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|x86.ActiveCfg = Release|Any CPU
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1}.Release|x86.Build.0 = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|x64.ActiveCfg = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|x64.Build.0 = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|x86.ActiveCfg = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Debug|x86.Build.0 = Debug|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|Any CPU.Build.0 = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|x64.ActiveCfg = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|x64.Build.0 = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|x86.ActiveCfg = Release|Any CPU
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -599,6 +627,8 @@ Global
{916370AB-B0D3-4136-850B-AA12FAB23ECD} = {4B000021-5278-4F2A-B734-DE49F55D4024}
{C5F9D191-CA3B-4648-B8A9-62E33B4622EB} = {4B000021-5278-4F2A-B734-DE49F55D4024}
{D9FFEC52-B701-4DB5-969C-BAC4F8EB220C} = {3079E458-D0E6-4F99-8CAB-80011D35C7DA}
{E88CDE01-A6E6-4FDF-8FC7-0A9B0D1C76C1} = {4B000021-5278-4F2A-B734-DE49F55D4024}
{0F854F2B-8F87-4D83-8EDE-F392D85C2D9C} = {3079E458-D0E6-4F99-8CAB-80011D35C7DA}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {9DD4022C-A010-4A9B-BCC5-171566D4CB17}
Expand Down
18 changes: 18 additions & 0 deletions src/System.Memory.Polyfill/System.Memory.Polyfill.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\..\tools\common.props" />
<PropertyGroup>
<Description>Adapters for Span APIs</Description>
<TargetFrameworks>netstandard1.3;netcoreapp2.1</TargetFrameworks>
<PackageTags>Span Memory Adapter</PackageTags>
<!-- disable automatic compile item inclusion so that we can set the correct metadata on TextTemplate-generated sources -->
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't actually have any of these, so you can remove this and the compile entry below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do.

<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Version="$(SystemMemoryVersion)" />
<PackageReference Include="System.Buffers" Version="$(SystemMemoryVersion)" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use SystemBuffersVersion instead of SystemMemoryVersion.

<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="$(SystemCompilerServicesUnsafeVersion)" />
</ItemGroup>
<ItemGroup>
<Compile Include="System\**\*.*" />
</ItemGroup>
</Project>
18 changes: 18 additions & 0 deletions src/System.Memory.Polyfill/System/Parse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System
{
public static partial class Int32Polyfill
{
public static bool TryParse(ReadOnlySpan<char> buffer, out int value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would,'t work for implicit cast from Span<char>?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not? It's not an extension method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, true... Is a bit ugly then :)

Static extension method would be better dotnet/csharplang#192 though alas not a thing...

{
#if NETCOREAPP2_1
return int.TryParse(buffer, out value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to avoid using the versioned define as it makes it more difficult if you decide to retarget. Instead use #if NETCOREAPP

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this api exists only in netcoreapp 2.1

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You aren't defining targeting here, you're just ifdefing the code. Imagine that you wanted to cross-compile for netcoreapp2.2 as well in the future, you'd need to touch this ifdef for no reason. I think you're more likely to cross-compile for a future version of netcoreapp than you are an older version that doesn't have this API.
Alternatively you can define your own feature flag that represents what you want and make sure that's defined as appropriate.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree it would work today, but it won't if we ever add new targets. Correct?

#else
return int.TryParse(buffer.ToString(), out value);
#endif
}
}
}
32 changes: 32 additions & 0 deletions src/System.Memory.Polyfill/System/Stream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.IO;

namespace System
{
public static partial class MemoryPolyfill
Copy link
Member

@ahsonkhan ahsonkhan Jun 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

StreamPolyfill instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want to minimize the number of extensions types.

{
public static int Read(this Stream stream, Span<byte> buffer)
{
#if NETCOREAPP2_1
return stream.Read(buffer);
#else
byte[] pooled = null;
try
{
pooled = ArrayPool<byte>.Shared.Rent(buffer.Length);
int read = stream.Read(pooled, 0, pooled.Length);
pooled.AsSpan(0, read).CopyTo(buffer);
return read;
}
finally
{
if(pooled != null) ArrayPool<byte>.Shared.Return(pooled);
}
#endif
}
}
}
20 changes: 20 additions & 0 deletions tests/System.Memory.Polyfill.Tests/Int32.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Xunit;

namespace System.Polyfill.Tests
{
public class Int32Tests
{
[Fact]
public void Int32TryParse()
{
char[] buffer = int.MaxValue.ToString().ToCharArray();
ReadOnlySpan<char> span = buffer.AsSpan();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadOnlySpan<char> span = int.MaxValue.ToString().AsSpan();?

Assert.True(Int32Polyfill.TryParse(span, out int value));
Assert.Equal(int.MaxValue, value);
}
}
}
28 changes: 28 additions & 0 deletions tests/System.Memory.Polyfill.Tests/StreamTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.IO;
using Xunit;

namespace System.Polyfill.Tests
{
public class StreamTests
{
[Fact]
public void StreamRead()
{
var buffer = new byte[100];
for (int i = 0; i < buffer.Length; i++) buffer[i] = (byte)i;

var stream = new MemoryStream(buffer);
var span = new Span<byte>(new byte[100]);

var read = stream.Read(span);
Assert.Equal(buffer.Length, read);
for (int i = 0; i < buffer.Length; i++)
{
Assert.Equal(i, span[i]);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\tools\common.props" />
<PropertyGroup>
<TargetFrameworks>net46;netcoreapp2.1</TargetFrameworks>
<AssemblyOriginatorKeyFile>../../tools/test_key.snk</AssemblyOriginatorKeyFile>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the properties from here on look unnecessary for a test project.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I just copied them from some other test, but I will remove/simplify. Thanks.

<SignAssembly>true</SignAssembly>
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
<Description></Description>
<Copyright>Microsoft Corporation, All rights reserved</Copyright>
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
</ItemGroup>
<!-- Project references -->
<ItemGroup>
<ProjectReference Include="..\..\src\System.Memory.Polyfill\System.Memory.Polyfill.csproj" />
</ItemGroup>
<!-- register for test discovery in Visual Studio -->
<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>
</Project>