lender đ
Lending Iterator: a niche, yet seemingly pervasive, antipattern[^1]. This crate provides one such implementation, 'utilizing' #84533 and #25860.
Ok, maybe 'antipattern' is a little tough, but let's spare the antagonizing examples, if you can avoid using lending iterators, you generally should. You should heed the counsel of Polonious: "Neither a borrower nor a lender be".
âšī¸A GAT implementation has been published at
gat-lending-iterator, go check it out if you can!
Forewarning, before you go on with this crate, you should consider using a more seasoned 'lending iterator' crate, like the lending-iterator or streaming-iterator crates.
Also, if a dyn Lender trait object is in your future, this crate definitely isn't going to work.
This crate was not made to be used in any sort of production code, so please, use at your own risk (Documentation be damned! Unsafe transmutes beware!).
Commonly known as a "lending iterator", a lender is a kind of iterator over items
that live at least as long as the mutable reference to the lender in a call to Lender::next().
In other words, a kind of iterator that lends an item one at a time, a pattern not implementable
by the current definition of Iterator which only encompasses iterators over items that live at least
as long as the iterators themselves, i.e. Self: 'this implies <Self as Iterator>::Item: 'this.
Examples
I present to you WindowsMut.
use *;
// Fibonacci sequence
let mut data = vec!;
data = 1;
WindowsMut
.for_each;
assert_eq!;
As all great standard examples are, a WindowsMut just for a Fibonacci sequence is actually a great example of what you shouldn't use lending iterators for.
Libraries can just provide Index and IndexMut on their collections and it's a lot of boilerplate for something a simple for loop can do.
let mut data = vec!;
data = 1;
for i in 2..data.len
assert_eq!;
So, let's look at a slightly more interesting example, LinesStr, an io::Lines with an Item of &str instead of String.
It's a good example of borrowing from the iterator itself.
use io;
use *;
let buf = with_capacity;
let mut lines = LinesStr ;
assert_eq!;
assert_eq!;
For most cases like this, you could just probably rely on the optimizer, i.e. reusing the same buffer instead of allocating a new one each time, but you see where we're going with this.
Implementing Lender
To implement Lender, first you'll need to implement the Lending trait for your type.
This is the equivalent provider of Iterator::Item for Lenders.
use *;
;
The lifetime parameter 'lend describes the lifetime of the Lend.
It works by using a default generic of &'lend Self which induces an implicit reference lifetime bound 'lend: 'this,
necessary for usage of higher-ranked trait bounds with Lend.
Next, you'll need to implement the Lender trait for your type, the lending equivalent of Iterator.
use *;
;
Lender provides all of the methods as Iterator, except Iterator::partition_in_place and Iterator::array_chunks,
and most provide the same functionality as the equivalent Iterator method.
Notable differences in behavior include Lender::next_chunk providing a lender instead of an array
and certain closures may require usage of the hrc!, hrc_mut!, hrc_once! (higher-ranked closure) macros,
which provide a stable replacement for the closure_lifetime_binder feature.
To provide a similar functionality to Iterator::array_chunks, the Lender::chunky method makes lenders nice and chunky đ.
Turn a lender into an iterator with Lender::cloned() where lend is Clone, Lender::copied() where lend is Copy,
Lender::owned() where lend is ToOwned,or Lender::iter() where the lender already satisfies the restrictions of Iterator.
Resources
Please check out the great resources below that helped me and many others learn about Rust and the lending iterator problem. Thank you to everyone!
- Sabrina Jewson's Blog for their awesome blog post on why lifetime GATs are not (yet) the solution to this problem, I highly recommend reading it.
- The awesome people on the Rust Users Forum in helping me understand the borrow checker and HRTBs better and being patient with me and other aspiring rustaceans as we try to learn more about Rust.
- Daniel Henry-Mantilla for writing
lending-iteratorand many other great crates and sharing their great work. - Everyone who's contributed to Rust for making such a great language and iterator library.
Unsafe & Transmutes Beware!!!
Many patterns in lending iterators require polonius-emulating unsafe code, but please, if you see any unsafe code that can be made safe, please let me know!
License
Licensed under either the MIT or Apache-2.0 license.
[^1]: An antipattern is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive.