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

Discrete model connector input types #1

Merged
merged 11 commits into from
Aug 24, 2023
Merged

Conversation

Caellian
Copy link
Owner

@Caellian Caellian commented Aug 21, 2023

Simulation shouldn't care about event types models receive past the validation step.

  • Validation should report any routes connecting invalid TypeIds.

Models should be able to take in (virtually) any number of inputs and provide any number of output lanes.

  • Due to language limitations (namely variadic generics) the actual number of supported inputs and outputs is capped at 12 like some tuple impls in std lib, that seems like a reasonable limit for now.

Models are 'static, as are input and output declarations. This restriction is necessary as changing input and output counts/types while a simulation is running makes it incredibly cumbersome to detect changes and handle model validation which would slow down the simulation for most normal use cases.

Erasing model inputs and outputs sacrifices performance when compared to previous design due to additional vtable lookups (untested), but vastly improves: DX, (incremental) compilation time and simplicity of the implementation.

  • Namely, specializing Model trait for 12×12 IO would require insane amounts of generated code to support.
    • If such a simulation performance is required it's better to write a simulation from scratch anyway with entirely discrete types.

Modelling inputs as a struct bears the limitation of requiring all inputs to be provided at once or user facing code having to shift through multiple Option wrapped values which isn't the nicest interface. And given that a macro can generate several traits to support a much nicer interface at the cost of very little compile time, using HeterogeneousTuple seems like a much nicer approach.

  • HeterogeneousTuple works much like variadic generics would anyway so it provides a nicer transition trajectory for adaptation of existing code base once Rust gets them.
  • HeterogeneousTuple approach depends on specialization, auto_traits, and negative_impls unstable features to work.

And finally, user facing code should be straightforward to implement which means:

  • No duplicate type declarations.
    • Most types should be inferred by the compiler by returned data types.
  • End-user doesn't need to specify any lifetimes.
  • Model implementation should be dead simple for simple models.

This commit removes a need to define a global Message enum type and
allows Models to handle any event type.

It doesn't compile yet though:
- Didn't work out lifetimes fully

It also doesn't do all that I want it to:
- Models should allow generic inputs and outputs - e.g. a buffer model
  should allow for type arg T which gets stored in a queue, not sure how
  that works out with the current system.

What IS handled is type erasure of model inputs and outputs, which
prevents having to specialize a lot of structures used by the simulation
to end-user defined Message types. This makes the library defined model
components easier to use in conjunction with multiple crates defining
their own models.

Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
@Caellian Caellian self-assigned this Aug 21, 2023
Fix a typo

Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
@Caellian Caellian force-pushed the dev/type_safe_connectors branch 3 times, most recently from a09bcd2 to cdf5b49 Compare August 21, 2023 22:32
Init macro crate

Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
@Caellian Caellian force-pushed the dev/type_safe_connectors branch from cdf5b49 to eb30a3b Compare August 21, 2023 22:33
Make model handle erased handlers

Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
Current implementation requires specifying that 's: 'static which isn't
correct.

Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
Add ModelStore struct for easier management of models

Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
@Caellian Caellian force-pushed the dev/type_safe_connectors branch from 0e574fd to c59cc8b Compare August 23, 2023 03:21
Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
This change required Model output_connectors to return a non-static
type.

Created a Queue model to try out type resolution. Seems to compile fine.

Removed Pin from Event data as Events should be unique and not moved
from within handlers. I'll see how that works out with type erasure when
I get to that.

Made OutputConnectorInfo briefer to construct.
Returned docs where they belong.

Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
Signed-off-by: Tin Svagelj <tin.svagelj@live.com>
@Caellian
Copy link
Owner Author

The described behavior has been achieved by erasing connector types before they're attached to models, which avoids the need for HeterogeneousTuple type which was hard to reason with on type system level.

While the current implementation is more verbose, it's A LOT easier to implement as it doesn't require compiler extensions and nowhere near as many unstable Rust features.

Impl of Models is simplified by using a proc_macro.

@Caellian Caellian changed the title WIP: Type safe model connectors Type safe model connectors Aug 24, 2023
@Caellian Caellian changed the title Type safe model connectors Discrete model connector input types Aug 24, 2023
@Caellian Caellian merged commit 3419236 into main Aug 24, 2023
@Caellian Caellian deleted the dev/type_safe_connectors branch August 24, 2023 07:47
@Caellian Caellian restored the dev/type_safe_connectors branch August 24, 2023 07:48
@Caellian Caellian deleted the dev/type_safe_connectors branch August 24, 2023 07:48
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.

1 participant