| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
#[diplomat::bridge] |
| 6 |
pub mod ffi { |
| 7 |
use alloc::boxed::Box; |
| 8 |
use core::ops::RangeInclusive; |
| 9 |
|
| 10 |
/// Result of a single iteration of [`CodePointRangeIterator`]. |
| 11 |
/// Logically can be considered to be an `Option<RangeInclusive<u32>>`, |
| 12 |
/// |
| 13 |
/// `start` and `end` represent an inclusive range of code points [start, end], |
| 14 |
/// and `done` will be true if the iterator has already finished. The last contentful |
| 15 |
/// iteration will NOT produce a range done=true, in other words `start` and `end` are useful |
| 16 |
/// values if and only if `done=false`. |
| 17 |
#[diplomat::out] |
| 18 |
pub struct CodePointRangeIteratorResult { |
| 19 |
pub start: u32, |
| 20 |
pub end: u32, |
| 21 |
pub done: bool, |
| 22 |
} |
| 23 |
|
| 24 |
/// An iterator over code point ranges, produced by `ICU4XCodePointSetData` or |
| 25 |
/// one of the `ICU4XCodePointMapData` types |
| 26 |
#[diplomat::opaque] |
| 27 |
pub struct CodePointRangeIterator<'a>(pub Box<dyn Iterator<Item = RangeInclusive<u32>> + 'a>); |
| 28 |
|
| 29 |
impl<'a> CodePointRangeIterator<'a> { |
| 30 |
/// Advance the iterator by one and return the next range. |
| 31 |
/// |
| 32 |
/// If the iterator is out of items, `done` will be true |
| 33 |
pub fn next(&mut self) -> CodePointRangeIteratorResult { |
| 34 |
self.0 |
| 35 |
.next() |
| 36 |
.map(|r| CodePointRangeIteratorResult { |
| 37 |
start: *r.start(), |
| 38 |
end: *r.end(), |
| 39 |
done: false, |
| 40 |
}) |
| 41 |
.unwrap_or(CodePointRangeIteratorResult { |
| 42 |
start: 0, |
| 43 |
end: 0, |
| 44 |
done: true, |
| 45 |
}) |
| 46 |
} |
| 47 |
} |
| 48 |
} |