Coder Social home page Coder Social logo

magicant / fuzed-iterator-rs Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 14 KB

Rust iterator wrapper for detecting incorrect use of non-fused iterators

Home Page: https://crates.io/crates/fuzed-iterator

License: Apache License 2.0

Rust 100.00%
debugging iterator rust

fuzed-iterator-rs's Introduction

Fuzed iterator

fuzed-iterator at crates.io fuzed-iterator at docs.rs Build status Changelog

fuzed-iterator is a Rust library crate that provides a simple iterator wrapper that causes a panic if the iterator is used after it has once returned None.

Many iterators in std implement the FusedIterator trait, which guarantees that the iterator will never return Some after returning None. This may make programmers incorrectly assume that any iterator works like a FusedIterator, leading to bugs when a non-fused iterator is used.

The Fuze wrapper provided by this crate can be used to wrap any iterator, causing a panic if the iterator is used after returning None for the first time. This can be used to catch bugs where a non-fused iterator is used in a context where a fused iterator is expected. In your unit tests (or integration tests), pass iterators through Fuze to your functions that expect an iterator (not necessarily fused), and the tests will panic if the function calls Iterator::next excessively.

Example

use fuzed_iterator::IteratorExt as _;
let mut iter = (0..3).fuze();
assert_eq!(iter.next(), Some(0));
assert_eq!(iter.next(), Some(1));
assert_eq!(iter.next(), Some(2));
assert_eq!(iter.next(), None);
iter.next(); // Another `next` call would panic!
/// Collects items from an iterator into a vector, but drops the first item.
fn drop_first_and_collect<I: IntoIterator<Item = i32>>(i: I) -> Vec<i32> {
    // This implementation is wrong because `next` may be called again even after it
    // returned `None`.
    let mut i = i.into_iter();
    _ = i.next();
    i.collect()
}

// Because of the wrong implementation, this test case would fail with a panic.
# /*
#[test]
# */
fn test_drop_first_and_collect_with_empty_array() {
    use fuzed_iterator::IteratorExt as _;
    let result = drop_first_and_collect([].into_iter().fuze());
    assert_eq!(result, []);
}
# test_drop_first_and_collect_with_empty_array();
/// Collects items from an iterator into a vector, but drops the first item.
fn drop_first_and_collect<I: IntoIterator<Item = i32>>(i: I) -> Vec<i32> {
    // This is the correct implementation.
    let mut i = i.into_iter();
    if i.next().is_none() {
        return vec![];
    }
    i.collect()
}

// Test passed!
# /*
#[test]
# */
fn test_drop_first_and_collect_with_empty_array() {
    use fuzed_iterator::IteratorExt as _;
    let result = drop_first_and_collect([].into_iter().fuze());
    assert_eq!(result, []);
}
# test_drop_first_and_collect_with_empty_array();

License

MIT or Apache 2.0, at your option

fuzed-iterator-rs's People

Contributors

magicant avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.