Crates.io | iter_view |
lib.rs | iter_view |
version | 0.1.4 |
source | src |
created_at | 2023-02-11 07:33:45.336385 |
updated_at | 2023-02-12 07:46:13.260858 |
description | Create iterator from readonly reference |
homepage | |
repository | https://github.com/redforks/iter_view |
max_upload_size | |
id | 782378 |
size | 12,987 |
Rust has IntoIterator
trait, to create Iterator
from a type. IntoIterator
consumes the type,
many types provides iter()
method to create Iterator
from immutable reference without consuming the type.
But no trait for iter()
method.
In most cases, such trait is not needed, because iter()
method returns Iterator
which implements IntoIterator
trait.
But consider the following case:
trait Inspector<T> {
fn inspect(&self, v: &T);
}
inspect()
method takes immutable reference of T
, and T
is not Copy
type. If impl
Inspector
for any object can convert into Iterator
, we can write code like this:
impl<T, I> Inspector<T> for I
where
I: IntoIterator<Item = T>,
T: std::fmt::Debug,
{
fn inspect(&self, v: &T) {
for item in self {
println!("{:?}", item);
}
}
}
But IntoIterator
trait consumes the type, compiler won't let it go.
If we impl Inspector
on slice:
impl<T: std::fmt::Debug> Inspector<T> for [T] {
fn inspect(&self, v: &T) {
for item in self {
println!("{:?}", item);
}
}
}
Won't work, unless we change Inspector
trait to allow unsized type:
trait Inspector<T: ?Sized> {
fn inspect(&self, v: &T);
}
Unsized types are poison, cause much trouble to use, we don't want to use it.
Vec<T>
support convert to slice very easily, but many other types don't support it, such as HashMap
,
LinkedList
, slice version can not cover all cases.
We can not pass Iterator
to inspect()
method, because Iterator
is a mutable object, and inspect()
requires immutable reference, make it impossible to work.
iter_view
crate provides IterView
trait, which is similar to IntoIterator
, but it doesn't consume the type,
pub trait IterView<'a> {
type Item: 'a;
type Iter: Iterator<Item = &'a Self::Item>;
fn iter(&'a self) -> Self::Iter;
}
Use IterView
trait, we can impl Inspector
for any type which implements IterView
:
impl<T, I> Inspector<T> for I
where
I: IterView<Item = T>,
T: std::fmt::Debug,
{
fn inspect(&self, v: &T) {
for item in self.iter() {
println!("{:?}", item);
}
}
}
License: MIT OR Apache-2.0