rust-dnsbox/lib/dnsbox-base/src/common_types/name/name_iterator.rs

51 lines
1.1 KiB
Rust

use super::{DnsLabelRef, DnsName};
/// Iterator type for [`DnsName::labels`]
///
/// [`DnsName::labels`]: struct.DnsName.html#method.labels
#[derive(Clone)]
pub struct DnsNameIterator<'a> {
pub(super) name: &'a DnsName,
pub(super) front_label: u8,
pub(super) back_label: u8,
}
impl<'a> Iterator for DnsNameIterator<'a> {
type Item = DnsLabelRef<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.front_label >= self.back_label {
return None;
}
let label = self.name.label_ref(self.front_label);
self.front_label += 1;
Some(label)
}
fn size_hint(&self) -> (usize, Option<usize>) {
let count = self.len();
(count, Some(count))
}
fn count(self) -> usize {
self.len()
}
}
impl<'a> ExactSizeIterator for DnsNameIterator<'a> {
fn len(&self) -> usize {
(self.back_label - self.front_label) as usize
}
}
impl<'a> DoubleEndedIterator for DnsNameIterator<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.front_label >= self.back_label {
return None;
}
self.back_label -= 1;
let label = self.name.label_ref(self.back_label);
Some(label)
}
}