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

feat(axevent)!: Improve callbacks #59

Merged
merged 4 commits into from
Oct 10, 2024
Merged

Conversation

apljungquist
Copy link
Contributor

@apljungquist apljungquist commented Jul 30, 2024

The summary of this commit is poor because I am being lazy and changing two things.

Box is removed from the type of subscription callbacks to make the interface nicer; the user does not have to type out Box::new(...) and there will generally be less indentation. One potential downside of this is that monomorphization of generic functions tend to increase the binary size and I suspect that callbacks are particularly bad since each callback gets its own type.

The type of declaration callbacks is constrained to make avoiding leaking the callbacks easier. It is probably possible to convert the FnOnce to an FnMut the same way we have done in other places but because the argument is a Box<dyn ...> this is not as straightforward and since I'd like to remove the box from the interface entirely it is not a detour I want to take.

The Deferred type is introduced to make it easier to ensure that callbacks are freed exactly once. Though convenient I think this pattern seems dangerous since it is not immediately obvious where the unsafe code is being called.

The summary of this commit is poor because I am being lazy and changing
two things.

Box is removed from the type of subscription callbacks to make the
interface nicer; the user does not have to type out `Box::new(...)`
and there will generally be less indentation. One potential downside
of this is that monomorphization of generic functions tend to increase
the binary size and I suspect that callbacks are particularly bad since
each callback gets its own type.

The type of declaration callbacks is constrained to make avoiding
leaking the callbacks easier. It is probably possible to convert the
`FnOnce` to an `FnMut` the same way we have done in other places but
because the argument is a `Box<dyn ...>` this is not as straightforward
and since I'd like to remove the box from the interface entirely it is
not a detour I want to take.

The `CallOnDrop` type is introduced to make it easier to ensure that
callbacks are freed exactly once. Though convenient I think this
pattern seems dangerous since it is not immediately obvious where the
unsafe code is being called.
@apljungquist apljungquist mentioned this pull request Oct 9, 2024
The intended benefit of this commit is to make the API slightly more
ergonomic by allowing users to omit `Box::new`.

Possible side effects that I have not investigated include:
- Better, worse or different compiler errors.
- Larger binaries due to monomorphization.
- Harder to package as rlib due to more generic methods.

`apps/send_event/src/main.rs`:
- Make the declaration callback `FnMut` even though I expect it to be
  called at most once because I have not confirmed this suspicion and
  implementing dropping it as `FnOnce` turned out to be difficult.
- Add a test ensuring that it continues to be possible to provide no
  callback. The main benefit of this is failing to compile if there is
  a regression, but running it makes it show up in test summaries which
  is nice.

`crates/axevent/src/flex.rs`:
- Add the `Deferred` helper type to make dropping the callbacks easier
  now that they do not have a knowable type.
- Guard the handlers with `Mutex` to keep the handler `Send` and `Sync`
  like the underlying C struct.
@apljungquist apljungquist marked this pull request as ready for review October 9, 2024 20:32
@apljungquist apljungquist requested a review from a team as a code owner October 9, 2024 20:32
@apljungquist apljungquist merged commit 5afd18c into main Oct 10, 2024
2 checks passed
@apljungquist apljungquist deleted the axevent_improve_callbacks branch October 10, 2024 08:11
apljungquist added a commit that referenced this pull request Oct 15, 2024
…per_and_example

* upstream/main:
  Update rust toolchain to 1.80.1 (#100)
  fix!: Make builds reproducible in dev-container (#99)
  feat(axevent)!: Use generic callbacks and remove leak (#59)
  fix: Install `cargo-acap-sdk` in venv (#97)
  chore: release cargo-acap-sdk 0.2.0 (#95)
  chore(deps): bump dirs from 3.0.2 to 5.0.1 (#85)
  Add `Cargo.lock` to generated files (#93)
  chore: Build apps with custom profile (#90)
  Remove leftover `dbg!` and copy `.eap` to appropriate names (#91)
  Use `cargo` with `--locked` option in checks (#92)
apljungquist added a commit that referenced this pull request Nov 5, 2024
* upstream/main: (48 commits)
  chore: Use workspace dependencies consistently (#126)
  fix(axevent_example): Return `Break` from signal handlers (#125)
  chore(deps): bump ghcr.io/devcontainers/features/common-utils (#124)
  feat(device-manager): Allow unsigned ACAPs (#122)
  feat(acap-build): Enable bypassing `acap-build` (#119)
  chore: streamline CI (#118)
  fix: Don't invalidate Cargo caches (#117)
  fix(acap-ssh-utils): Warn users that commands may break (#114)
  feat: Factor out `acap-build` wrapper to lib crate (#112)
  feat: Add `fleet-manager` program (#111)
  feat: Add edge storage wrapper and example (#108)
  fix: Measure apparent size (#107)
  chore(acap-logging): Bump version (#106)
  fix(cargo-acap-sdk): Set executable bits (#105)
  fix(acap-logging)!: Disable default features for env_logger (#103)
  chore: Track size of apps (#102)
  chore: Don't use clearly defined for license info (#101)
  Update rust toolchain to 1.80.1 (#100)
  fix!: Make builds reproducible in dev-container (#99)
  feat(axevent)!: Use generic callbacks and remove leak (#59)
  ...
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