Skip to content

std.io: add AnyWriter#17879

Closed
torque wants to merge 1 commit intoziglang:masterfrom
epicyclic-dev:anywriter
Closed

std.io: add AnyWriter#17879
torque wants to merge 1 commit intoziglang:masterfrom
epicyclic-dev:anywriter

Conversation

@torque
Copy link
Contributor

@torque torque commented Nov 5, 2023

This is a non-generic writer that holds a type-erased pointer to a generic writer and is suitable for use in scenarios, such as runtime callbacks, where generic functions cannot be used.

The approach taken here is a bit different from AnyReader in that the type-erased version has not replaced the generic version. Instead, an interface mixin is used to share the same interface between the two implementations.

Some additional thoughts:

  • I wasn't sure where to add tests, so I put one in the general std.io test file. The test coverage here is pretty minimal since I didn't see a lot of other writer tests to be used as a template. This could be fleshed out.
  • I opted for the mixin approach because I think the pub usingnamespace pattern for interface mixins is really nice and would like to see it used more in std. I think it's odd that AnyReader and GenericReader implement the same interface manually, but GenericReader internally creates an AnyReader for almost all of its methods. I am curious what the general opinion on this approach is.
  • The AnyWriter could have more methods in its vtable, which would reduce the number of function calls through pointers at the cost of additional code complexity. Since IO is expected to be orders of magnitude slower than a virtual call, I think it makes sense to keep the code simple. Also, maybe the optimizer is smart enough to optimize these out in many cases?

This is a non-generic writer that holds a type-erased pointer to a
generic writer and is suitable for use in scenarios, such as runtime
callbacks, where generic functions cannot be used.

The approach taken here is a bit different from AnyReader in that the
type-erased version has not replaced the generic version. Instead, an
interface mixin is used to share the same interface between the two
implementations.
@Vexu
Copy link
Member

Vexu commented Nov 5, 2023

Duplicate of #17634

@Vexu Vexu marked this as a duplicate of #17634 Nov 5, 2023
@torque
Copy link
Contributor Author

torque commented Nov 5, 2023

Alas, my inability to search has let me down once again. However, the approaches of the two PRs are quite different, so I'll leave this as-is for someone else to decide its fate.

@xdBronch
Copy link
Contributor

xdBronch commented Feb 8, 2024

should be closed now that #17634 is merged
and fwiw i imagine this pr would very quickly get rejected due to the use of usingnamespace

@Vexu Vexu closed this Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants