Skip to content

Use impl Future instead of Box<dyn Future>#1698

Open
sdroege wants to merge 1 commit intogtk-rs:mainfrom
sdroege:impl-future
Open

Use impl Future instead of Box<dyn Future>#1698
sdroege wants to merge 1 commit intogtk-rs:mainfrom
sdroege:impl-future

Conversation

@sdroege
Copy link
Member

@sdroege sdroege commented Jan 7, 2026

@A6GibKm

Any opinions on this one? I don't know how useful it is in practice. It might cause longer compile times and code bloat, or maybe the opposite.

We can't use async fn in traits because that gives compiler warnings (it's discouraged in public traits).

@sdroege sdroege requested a review from bilelmoussaoui January 7, 2026 09:08
@sdroege
Copy link
Member Author

sdroege commented Jan 7, 2026

An alternative here would be to do impl Future but still Box::pin the futures (which would be an implementation detail then). That hides this implementation detail and doesn't change anything with regards to generated code.

@sdroege
Copy link
Member Author

sdroege commented Jan 11, 2026

So let's list the 4 options:

  1. Keep things Pin<Box<dyn Future>>:
    • (-) Heap allocation, dynamic dispatch
    • (+) Nameable type, less code bloat
  2. impl Future backed by Pin<Box<dyn Future>>:
    • (-) Heap allocation, dynamic dispatch, unnameable type
    • (+) Less code bloat, doesn't leak minor implementation details (boxed pinned future can be replaced by anything else later)
  3. impl Future backed by the plain GioFuture
    • (-) Unnameable type, code bloat
    • (+) Doesn't leak minor implementation details, can be inlined (but questionable how useful: GioFuture is basically a oneshot channel)
  4. async fn backed by the plain GioFuture
    • (-) Unnameable type, code bloat, gives compiler warnings in public traits
    • (+) Doesn't leak minor implementation details, can be inlined (but questionable how useful: GioFuture is basically a oneshot channel), Looks more natural

I think out of these, only 1. and 3. are really useful. The main question there is the actual amount of code bloat and how often the return type is used by name (there are some uses inside gio for converting futures into streams which might also be solvable differently).

I'd say to defer this to a future breaking release.

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