# virtualizer
[](https://github.com/Latias94/virtualizer/actions/workflows/ci.yml)
[](https://crates.io/crates/virtualizer)
[](https://docs.rs/virtualizer)
[](https://www.rust-lang.org/)
[](LICENSE-MIT)
A headless virtualization engine inspired by TanStack Virtual.
This crate provides the core algorithms needed to virtualize massive lists at interactive frame
rates:
- Prefix sums over item sizes (Fenwick tree).
- Fast offset → index lookup.
- Overscanned visible ranges.
- Optional dynamic measurement via `measure` / `resize_item`.
- Scroll-to-index helpers (`scroll_to_index_offset`, `Align`).
It is UI-agnostic and can be used in TUIs/GUI frameworks.
## Usage
```rust
use virtualizer::{Align, Virtualizer, VirtualizerOptions};
v.for_each_virtual_item(|it| items.push(it));
assert!(!items.is_empty());
let off = v.scroll_to_index_offset(1234, Align::Start);
v.set_scroll_offset(off);
```
If you want clamped scroll offsets (useful for scroll-to helpers), use `set_scroll_offset_clamped`
or `clamp_scroll_offset`.
If your UI layer wants keys alongside items, use `for_each_virtual_item_keyed`.
If you want to scroll to a raw offset (TanStack `scrollToOffset`), set it via
`set_scroll_offset_clamped` (or `set_scroll_offset` if you want to handle clamping yourself).
For an adapter-style walkthrough (rect/scroll events/dynamic measurement), see
`examples/adapter_sim.rs` (`cargo run -p virtualizer --example adapter_sim`).
For more end-to-end adapter scenarios (tween scrolling, pinned headers, keyed reorders, dynamic
measurement), see `examples/README.md`.
If you want adapter-level utilities (scroll anchoring for prepend, tween helpers), see the optional
`virtualizer-adapter` crate in this workspace (`../virtualizer-adapter/`).
### Reusing buffers (allocation-friendly)
If you call `for_each_virtual_item` every frame, consider reusing a `Vec` to avoid per-frame
allocations:
```rust
use virtualizer::{Virtualizer, VirtualizerOptions};
Use `VirtualizerOptions::new_with_key` if you want a non-`u64` key type (e.g. for stable caching
across reorders in your UI layer).
```rust
use virtualizer::{Virtualizer, VirtualizerOptions};
If your scroll offset is measured from a larger scroll container (e.g. window scrolling) and the
list starts after some header/content, set `scroll_margin` so virtual item `start` values match the
scroll container coordinate system:
```rust
use virtualizer::{Virtualizer, VirtualizerOptions};
internally batch and mark scrolling:
- `apply_scroll_offset_event` / `apply_scroll_offset_event_clamped`
- `apply_scroll_frame` / `apply_scroll_frame_clamped`
### no_std + alloc
Disable default features to build in `no_std` environments (requires `alloc`):
```sh
cargo build --no-default-features
```
## License
Dual-licensed under `MIT OR Apache-2.0`. See `LICENSE-MIT` and `LICENSE-APACHE`.