Skip to content

[API Proposal]: void-returning Encode callback for AsnWriter #112675

Closed
@vcsjones

Description

@vcsjones

Background and motivation

In #75759 we approved two APIs that use a callback mechanism for encoding when using AsnWriter. Both of the added methods return a TReturn.

When wiring this in through the Libraries, there are several places that look like this:

inner.Encode(writer, static (writer, encoded) =>
{
writer.WriteBitString(encoded);
return (object?)null;
});

They modify some state that gets passed in, but don't need to return anything, which results in a return (object?)null.

We should overload Encode to accept an Action callback and return void.

API Proposal

namespace System.Formats.Asn1;

public partial class AsnWriter {
#if NET9_0_OR_GREATER
    public void Encode<TState>(
        TState state,
        Action<TState, ReadOnlySpan<byte>> encodeCallback) where TState : allows ref struct;
#endif
}

API Usage

From the example above, that would become a nicer-looking:

inner.Encode(writer, static (writer, encoded) =>
{
    writer.WriteBitString(encoded);
});

Alternative Designs

There is, yet another, overload we could add - which is one that accepts no state and returns nothing. I don't know how useful that is. That would imply that global state is being modified, or something captured.

That would look like:

public void Encode(Action<ReadOnlySpan<byte>> encodeCallback);

But I see little utility in it. It can be added for symmetry if that is important.

Risks

No response

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Formats.Asn1in-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions