vezel-dev / cathode

A terminal-centric replacement for the .NET console APIs.
https://docs.vezel.dev/cathode
BSD Zero Clause License
91 stars 7 forks source link

Add ControlBuilder.Memory #192

Closed scottbilas closed 2 months ago

scottbilas commented 2 months ago

Span<> isn't available inside of an async method, which makes it more difficult to use ControlBuilder without an alloc.

The underlying type is ArrayBufferWriter<>, which exports a Memory, so this change just exposes that API to ControlBuilder as well.

alexrp commented 2 months ago

I don't have any particular problem with taking this change, but just out of curiosity, what's the situation that made the Memory property necessary?

scottbilas commented 2 months ago

The use case is when wanting to pass ControlBuilder memory to an async utility function. The compiler will not permit receiving it as a ReadOnlySpan<char>. So ControlBuilder.Span is unusable.

A good example of this is Terminal.OutAsync(ReadOnlyMemory<char>). I can't get at the underlying ArrayBufferWriter array memory in the ControlBuilder, so the only other option is an unfortunate allocating ToString().