Skip to content
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]: MemoryMarshal.CreateReadOnlySpan that takes in param #83191

Closed
benaadams opened this issue Mar 9, 2023 · 5 comments
Closed
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory

Comments

@benaadams
Copy link
Member

Background and motivation

Currently CreateReadOnlySpan takes a ref so if a readonly field is passed it needs to first go via Unsafe.AsRef(in which seems an unnecessary friction of converting the type to non readonly.

API Proposal

namespace System.Runtime.InteropServices;

public static partial class MemoryMarshal
{
    public static ReadOnlySpan<T> CreateReadOnlySpan<T>(scoped in T reference, int length);
}

API Usage

// Fancy the value
readonly int _value;

MemoryMarshal.CreateReadOnlySpan(in _value, 1);

Alternative Designs

No response

Risks

No response

@benaadams benaadams added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Mar 9, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 9, 2023
@ghost
Copy link

ghost commented Mar 9, 2023

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

Currently CreateReadOnlySpan takes a ref so if a readonly field is passed it needs to first go via Unsafe.AsRef(in which seems an unnecessary friction of converting the type to non readonly.

API Proposal

namespace System.Runtime.InteropServices;

public static partial class MemoryMarshal
{
    public static ReadOnlySpan<T> CreateReadOnlySpan<T>(scoped in T reference, int length);
}

API Usage

// Fancy the value
readonly int _value;

MemoryMarshal.CreateReadOnlySpan(in _value, 1);

Alternative Designs

No response

Risks

No response

Author: benaadams
Assignees: -
Labels:

api-suggestion, area-System.Memory

Milestone: -

@Sergio0694
Copy link
Contributor

I think the plan is to change the existing API to take a ref readonly reference (see here), rather than adding a new API. One of the main reasons of ref readonly is specifically to allow changing the signature of existing API without breaking code. Also in technically would be incorrect as it would also allow rvalues, which shouldn't be used to create a span 🙂

@stephentoub
Copy link
Member

Yup, what @Sergio0694 said. And even if we wanted to we couldn't add the proposed method; we can't overload based on ref vs in.

@tannergooding
Copy link
Member

CC. @jaredpar since he's championing the issue.

Just an FYI that this is "another" case that would be switched from ref to ref readonly if possible.

@tannergooding
Copy link
Member

Closing this as inactionable at the moment. We can revisit if the language adds support for ref readonly

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Mar 10, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory
Projects
None yet
Development

No branches or pull requests

4 participants