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

Switch from impl ExtendsC for E to impl ExtendableFrom<E> for C #879

Open
Friz64 opened this issue Mar 16, 2024 · 1 comment
Open

Switch from impl ExtendsC for E to impl ExtendableFrom<E> for C #879

Friz64 opened this issue Mar 16, 2024 · 1 comment

Comments

@Friz64
Copy link
Contributor

Friz64 commented Mar 16, 2024

Here I use C for "carrier" and E for "element". A carrier is the struct that gets consumed by Vulkan API functions, and is the head of the pointer chain. An element is the opposite, just a part of the pointer chain.

The elements a carrier may be extended by is defined by the Vulkan registry. We leverage this information to provide a compile-time safe API, by generating a push_next builder function for every carrier which only takes elements that implement ExtendsC (a trait only implemented for valid elements).

In erupt, I switched this around: The trait is now implemented for the carriers, and the elements are in the generic parameter position. This means that the push_next function can now become a trait function with a default implementation, which works everywhere.

See the ExtendableFrom trait from erupt.

Side note: ash generates a separate trait for every carrier struct, where it could use generics (Extends<C>) instead.

@Ralith
Copy link
Collaborator

Ralith commented Mar 16, 2024

the push_next function can now become a trait function with a default implementation

This is a major drawback:

  • Trait functions must be imported explicitly, whereas inherent functions are always available
  • A trait method that is never implemented is misleading, normally only justified in extension traits

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

No branches or pull requests

2 participants