Skip to content

Support writing binary data from ReadOnlyMemory<byte> #2392

Closed

Description

@roji

Originally raised in #2390. Writing from ReadOnlySpan<byte> isn't going to be possible as it can't exist on the heap (and ADO.NET requires NpgsqlParameter.Value to be set to it).

/cc @bgrainger @divega @ajcvickers @bricelam

Activity

  1. added this to the 4.1 milestone on Mar 26, 2019
  2. self-assigned this
    on Mar 26, 2019
  3. changed the title [-]Support writing binary data from Memory<byte>[/-] [+]Support writing binary data from ReadOnlyMemory<byte>[/+] on Mar 26, 2019
  4. bgrainger commented on Mar 27, 2019

    @bgrainger
    Contributor

    Is there any reason in principle to not also support DbCommand.Value DbParameter.Value being set to Memory<byte> or ArraySegment<byte>, as a convenience? (Counterargument: no one's ever asked for it; once ReadOnlyMemory<byte> is supported, it's easy to add .AsMemory() at the call site as a workaround.)

  5. roji commented on Mar 27, 2019

    @roji
    MemberAuthor

    DbCommand.Value

    You mean DbParameter.Value, right?

    being set to Memory or ArraySegment, as a convenience?

    You're asking about supporting Memory<byte> in addition to ReadOnlyMemory<byte>, right? If so, in general, write APIs seem to accept the read-only types (e.g. Stream.Write(ReadOnlySpan), with an implicit cast making the transition easier. It's true that ADO.NET is a bit more tricky, since you set Value of type object, so implicit casting doesn't work and users will have to do it explicitly - except if you use the typed NpgsqlParameter<T>. So I'm not too sure, maybe we should wait for someone to complain first?

    PS Npgsql already supports writing ArraySegment<byte>.

  6. bgrainger commented on Mar 27, 2019

    @bgrainger
    Contributor

    You mean DbParameter.Value, right?

    Yes, fixed.

    If so, in general, write APIs seem to accept the read-only types

    It's fairly common for DbParameter.Value to be bound to byte[], which is not a read-only type. (Nor is ArraySegment<byte>.) Supporting Memory<byte> seemed similar (and avoided the casting problems).

    PS Npgsql already supports writing ArraySegment<byte>.

    Good to know. MySqlConnector does now, too. :)

  7. roji commented on Mar 27, 2019

    @roji
    MemberAuthor

    If so, in general, write APIs seem to accept the read-only types

    It's fairly common for DbParameter.Value to be bound to byte[], which is not a read-only type. (Nor is ArraySegment.) Supporting Memory seemed similar (and avoided the casting problems).

    That's true, but that's because there's no read-only alternative to byte[]. It seems that for all Span/Memory APIs which were added to the BCL, the read-only version is accepted for write operations. Again, this doesn't make a big difference for BCL APIs since there's an implicit cast.

  8. roji commented on Apr 18, 2019

    @roji
    MemberAuthor

    Note: this option will only be available when targeting .NET Standard 2.1, since there's no (efficient) way to write Memory to Socket/NetworkStream in .NET Standard 2.0. We could in theory target a version of .NET Core under 3.0, but that seems a bit unnecessary.

  9. added a commit that references this issue on Apr 18, 2019
    7cc28ae
  10. added a commit that references this issue on May 3, 2019
    951dc83
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions