-
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]: IReadOnlyMemoryOwner<T> to indicate buffer ownership and provide read-only access #110947
Comments
Tagging subscribers to this area: @dotnet/area-system-memory |
Why not just take a var buffer = MemoryPool<T>.Shared.Rent();
// populate buffer with data
new DataConsumer().ConsumeData(buffer.Memory);
public class DataConsumer
{
public void ConsumeData(ReadOnlyMemory<T> buffer)
{
// work with buffer
}
} |
|
This restriction does not sound right; the "owner" of a resource should be able to do anything with it. Scenarios where a memory owner is actually unable to write to it revolve around unmanaged memory, for which |
I'm using the terms "own" and "ownership" because these are the wordings used in the official usage guidelines. But my understanding is that "owning" a |
Background and motivation
Both
Span<T>
andMemory<T>
have read-only counterparts (ReadOnlySpan<T>
andReadOnlyMemory<T>
) to provide read-only access butIMemoryOwner<T>
currently does not.When using
IMemoryOwner<T>
it is quite often that we need to transfer ownership (i.e. responsibility to dispose) and read access, but we do not intend to give the recipient write capabilities. For example, buffer could be transferred from a data producing class to a data consuming class, the latter is expected to be only reading the data and disposing of it at the end of its consumption. In such cases anIReadOnlyMemoryOwner<T>
interface which provides access to aReadOnlyMemory<T>
will better document the purpose and prevent misuse.API Proposal
API Usage
Alternative Designs
No response
Risks
No response
The text was updated successfully, but these errors were encountered: