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

Memory/ReadOnlyMemory cast/extensions (as Span) #23669

Closed
benaadams opened this issue Sep 28, 2017 · 5 comments
Closed

Memory/ReadOnlyMemory cast/extensions (as Span) #23669

benaadams opened this issue Sep 28, 2017 · 5 comments
Milestone

Comments

@benaadams
Copy link
Member

Parallel the Span<T> cast extensions for Memory<T>

e.g.

static Span<byte> AsBytes<T>(this Span<T> source) where T : struct
static ReadOnlySpan<byte> AsBytes<T>(this ReadOnlySpan<T> source) where T : struct
static Span<TTo> NonPortableCast<TFrom, TTo>(this Span<TFrom> source) where TFrom : struct where TTo : struct
static ReadOnlySpan<TTo> NonPortableCast<TFrom, TTo>(this ReadOnlySpan<TFrom> source) where TFrom : struct where TTo : struct

i.e

static Memory<byte> AsBytes<T>(this Memory<T> source) where T : struct
static ReadOnlyMemory<byte> AsBytes<T>(this ReadOnlyMemory<T> source) where T : struct
static Memory<TTo> NonPortableCast<TFrom, TTo>(this Memory<TFrom> source) where TFrom : struct where TTo : struct
static ReadOnlySpan<TTo> NonPortableCast<TFrom, TTo>(this ReadOnlyMemory<TFrom> source) where TFrom : struct where TTo : struct

/cc @KrzysztofCwalina @stephentoub @davidfowl

@KrzysztofCwalina
Copy link
Member

I think AsBytes is fine. Do we need the NonPortableCast? Such an ugly name.

cc: @jkotas, @ahsonkhan

@benaadams
Copy link
Member Author

To convert from Bytes to something else?

@stephentoub
Copy link
Member

stephentoub commented Sep 28, 2017

What does the implementation of AsBytes look like? e.g. if I construct a Memory<int> from an int[], and I then use AsBytes to get a Memory<byte>, what is the object stored in the Memory<byte>? What do I get out of its TryGetArray?

@benaadams
Copy link
Member Author

benaadams commented Sep 28, 2017

What do I get out of its TryGetArray?

Would need to allocate an indirection OwnedMemory<T> to nullify the return 😢

So probably worse getting a Span from the Memory, converting the Span and copying to a pooled array and using that

@benaadams
Copy link
Member Author

Yeah isn't going to work as can't put a reference of general "TFrom" in OwnedMemory<TTo> and get it to work

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants