Skip to content

Conversation

@Frando
Copy link
Member

@Frando Frando commented Nov 10, 2025

Description

This adds two new structs that allow to create watchers without having to store the full current value inline.

  • A LazyDirect that works like Direct only that it doesn't store the current value inline, only the epoch. It requires T: Default and returns T::default() when the underlying watchable has been dropped.
  • A WeakWatcher that contains only a weak pointer to the shared state and thus is only a single pointer size long. It has upgrade() -> Option<Direct> and lazy_upgrade() -> LazyDirect functions to upgrade the WeakWatcher to a watcher.

With these two additions, you can use watchers for big values without increasing the size of the structs where the watcher is stored on.

Breaking Changes

Notes & open questions

Change checklist

  • Self-review.
  • Documentation updates following the style guide, if relevant.
  • Tests if relevant.
  • All breaking changes documented.

@Frando Frando force-pushed the Frando/lazy-direct branch from a9ae952 to e13742e Compare November 10, 2025 11:56
@github-actions
Copy link

github-actions bot commented Nov 10, 2025

Documentation for this PR has been generated and is available at: https://n0-computer.github.io/n0-watcher/pr/31/docs/n0_watcher/

Last updated: 2025-11-10T12:18:07Z

@Frando Frando changed the title feat: add LazyDirect watcher feat: add LazyDirect and WeakWatcher Nov 10, 2025
@Frando Frando requested a review from matheus23 November 10, 2025 12:29
@matheus23
Copy link
Member

Unfortunately the latest changes to the Watcher trait require you to implement a fn peek(&self) -> &Self::Value, which makes implementing LazyWatcher impossible without storing a current: Self::Value in the struct.

The downstream use-case for this ended up using a Box<impl Watcher> instead of LazyWatcher to avoid ballooning struct sizes.

Closing this for these reasons ✌️

@matheus23 matheus23 closed this Nov 12, 2025
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.

3 participants