kas_text/
util.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License in the LICENSE-APACHE file or at:
4//     https://www.apache.org/licenses/LICENSE-2.0
5
6//! Utility types and traits
7
8/// Describes the state-of-preparation of a [`TextDisplay`][crate::TextDisplay]
9#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Ord, PartialOrd, Hash)]
10pub enum Status {
11    /// Nothing done yet
12    #[default]
13    New,
14    /// As [`Self::LevelRuns`], except these need resizing
15    ResizeLevelRuns,
16    /// Source text has been broken into level runs
17    LevelRuns,
18    /// Line wrapping and horizontal alignment is done
19    Wrapped,
20    /// The text is ready for display
21    Ready,
22}
23
24impl Status {
25    /// True if status is `Status::Ready`
26    #[inline]
27    pub fn is_ready(&self) -> bool {
28        *self == Status::Ready
29    }
30}
31
32/// An iterator over a `Vec` which clones elements
33pub struct OwningVecIter<T: Clone> {
34    v: Vec<T>,
35    i: usize,
36}
37
38impl<T: Clone> OwningVecIter<T> {
39    /// Construct from a `Vec`
40    pub fn new(v: Vec<T>) -> Self {
41        let i = 0;
42        OwningVecIter { v, i }
43    }
44}
45
46impl<T: Clone> Iterator for OwningVecIter<T> {
47    type Item = T;
48    fn next(&mut self) -> Option<Self::Item> {
49        if self.i < self.v.len() {
50            let item = self.v[self.i].clone();
51            self.i += 1;
52            Some(item)
53        } else {
54            None
55        }
56    }
57
58    fn size_hint(&self) -> (usize, Option<usize>) {
59        let len = self.v.len() - self.i;
60        (len, Some(len))
61    }
62}
63
64impl<T: Clone> ExactSizeIterator for OwningVecIter<T> {}
65impl<T: Clone> std::iter::FusedIterator for OwningVecIter<T> {}