Skip to content

Commit

Permalink
First attempt at basic axum-style state management
Browse files Browse the repository at this point in the history
Empty service/handler impl

I'm starting to understand the basis of Tower's service model.
However, that does not make this attempt good.
This only covers the case of a function that takes no arguments (and that
is on purpose).

In order to take more arguments, I will need to create a trait which can
handle them, think something like Axum's `FromRequest`. We will need an
adapted form of this, since we actually will want it for `FromEvent`.
You might be wondering why we don't just use `From<Event>`, but that is
annoyingly problematic, since you will get conflicting implementations
because "downstream crates may implement this for ..."; as far as I can
tell, this will require a custom trait.

I have not yet found a way around this.

Add Handler impl for up to 3 args

Ah ha! I found the secret! So it's true that you can not just take in
any argument for the first one. That's essentially special-cased.

But, assuming that most functions' first argument would be the event
itself (atspi::Event = Request), then we can make that the first
argument, followed by N arguments that implement `From<State>`
(roughly).

This will allow us to implement `From<State>` for various types, which
will be wrappers of more widely available types, for example:

- Current/LastFocusedItem(CacheItem)
- Current/LastCursorPos(CacheItem, u32)
- EventItem(CacheItem)
- etc.

Add generic first parameter for Handler

This allows the first parameter to be any type that implements
`TryFrom<atspi::Event>`. It currently requires an unwrap due the
possibility of it being the wrong event type. This should be converted
into its own Layer type, which can easily wrap the inner service,
instead of manually implementing the conversion within the `Service`
impl for `Handler<_>` types.

Add a quick breakdown, in English, of the trait bounds of a Handler impl

formatting
  • Loading branch information
TTWNO committed Mar 19, 2024
1 parent b77a72d commit 434abe9
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 43 deletions.
141 changes: 98 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions odilia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ zbus.workspace = true
odilia-notify = { version = "0.1.0", path = "../odilia-notify" }
clap = { version = "4.5.1", features = ["derive"] }
tokio-util.workspace=true
tower = { version = "0.4.13", features = ["util"] }

[dev-dependencies]
lazy_static = "1.4.0"
Expand Down
1 change: 1 addition & 0 deletions odilia/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod cli;
mod events;
mod logging;
mod state;
mod tower;

use std::{process::exit, sync::Arc, time::Duration};

Expand Down
Loading

0 comments on commit 434abe9

Please sign in to comment.