-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Span<byte> constructor for MemoryStream #72723
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivationA memory stream should work with a span of bytes. When you have a span of bytes as your data and need a MemoryStream for another api you shouldn't need to copy the data from the span to a new byte array to get a "view" in form of a memory stream to it. This conversion from one type of memory view to another should be seamlessly possible. API Proposalnamespace System.IO;
public class MemoryStream : Stream
{
public MemoryStream(Span<byte> buffer)
{
...
}
...
} API UsageSpan<byte> someData = ...;
using MemoryStream ms = new MemoryStream(someData);
//The FromImage is just an example for an API that uses a stream and doesn't take a span. I don't propose a change to the creation of an Image from bytes.
Image imgFromData = Image.FromStream(ms); Alternative DesignsAlternatively the byte array constructor of MemoryStream could be changed to a Span with likely braking no code since a byte array can implicitly be converted to a Span. RisksNo response
|
MemoryStream has to copy your data to its field on the heap so you can't avoid it. |
If you really need a |
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsBackground and motivationA memory stream should work with a span of bytes. When you have a span of bytes as your data and need a MemoryStream for another api you shouldn't need to copy the data from the span to a new byte array to get a "view" in form of a memory stream to it. This conversion from one type of memory view to another should be seamlessly possible. API Proposalnamespace System.IO;
public class MemoryStream : Stream
{
public MemoryStream(Span<byte> buffer)
{
...
}
...
} API UsageSpan<byte> someData = ...;
using MemoryStream ms = new MemoryStream(someData);
//The FromImage is just an example for an API that uses a stream and doesn't take a span. I don't propose a change to the creation of an Image from bytes.
Image imgFromData = Image.FromStream(ms); Alternative DesignsAlternatively the byte array constructor of MemoryStream could be changed to a Span with likely braking no code since a byte array can implicitly be converted to a Span. RisksNo response
|
A A stream that reads a |
Background and motivation
A memory stream should work with a span of bytes. When you have a span of bytes as your data and need a MemoryStream for another api you shouldn't need to copy the data from the span to a new byte array to get a "view" in form of a memory stream to it. This conversion from one type of memory view to another should be seamlessly possible.
API Proposal
API Usage
Alternative Designs
Alternatively the byte array constructor of MemoryStream could be changed to a Span with likely braking no code since a byte array can implicitly be converted to a Span.
Risks
No response
The text was updated successfully, but these errors were encountered: