Circular Cursors impl

I am currently building a TUI where I would like to put the current selected element in a cursor like structure. However I would like it to be circular so that when I reach the end it just starts at the beginning again.

I have modified the following example from the std docs to include the behavior I would like.

use std::io::Cursor;
use std::io::prelude::*;
use std::io::SeekFrom;

let mut buff = Cursor::new(vec![1, 2, 3, 4, 5]);

assert_eq!(buff.position(), 0);;
assert_eq!(buff.position(), 4);;
assert_eq!(buff.position(), 0);

What is the most idiomatic way to do this in rust?

I'm not sure if I understand the question.

If you track which UI element is selected, you probably don't need any clever data structure for it, just a bit of logic that wraps it around. It may be as simple as if selected_index < 0 {selected_index = last}.

OTOH if you need a circular buffer for storage, then there's

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.