Giter VIP home page Giter VIP logo

voncount's Introduction

Build Status Rust Version 1.13+ Crate Docs

Welcome to voncount - a Rust crate for counters.

Like the lovable Count von Count from Sesame Street, the voncount crate loves to count things. We provide the Counter trait which can be implemented on types which try to count things. We also provide two structs which implement the Counter trait:

  • ReadCounter
  • WriteCounter

voncount's People

Contributors

npmccallum avatar

Watchers

 avatar James Cloos avatar  avatar

voncount's Issues

Provide an example

I tried to use this crate, but struggled a lot because I couldn't get my code to work:

martin.mein-iserv.de ~/git/rust/bufreader (master) # cat src/main.rs 
use std::fs::File;
use std::io::BufReader;
use std::io::Error;
use std::io::BufRead;
use voncount::ReadCounter;
use voncount::Counter;

fn main() -> Result<(), Error>{
    let fh = File::open("test.txt")?;
    let readcounter = ReadCounter::from(fh);
    let reader = BufReader::new(readcounter);

    for line in reader.lines()
    {
        println!("line: {:?}", line);
    }

    println!("read bytes: {}", readcounter.count());

    Ok(())
}

And the error message wasn't particularly helpful:

martin.mein-iserv.de ~/git/rust/bufreader (master) # cargo run      
   Compiling bufreader v0.1.0 (/root/git/rust/bufreader)
error[E0277]: the trait bound `ReadCounter<'_, _>: From<File>` is not satisfied
  --> src/main.rs:10:23
   |
10 |     let readcounter = ReadCounter::from(fh);
   |                       ^^^^^^^^^^^ the trait `From<File>` is not implemented for `ReadCounter<'_, _>`
   |
   = help: the trait `From<&mut _>` is implemented for `ReadCounter<'_, _>`
   = help: for that trait implementation, expected `&mut _`, found `File`

For more information about this error, try `rustc --explain E0277`.
error: could not compile `bufreader` (bin "bufreader") due to 1 previous error

After a while, I finally figured it out - I need to pass mutable references to ReadCounter and BufReader:

martin.mein-iserv.de ~/git/rust/bufreader (master) # cat src/main.rs
use std::fs::File;
use std::io::BufReader;
use std::io::Error;
use std::io::BufRead;
use voncount::ReadCounter;
use voncount::Counter;

fn main() -> Result<(), Error>{
    let mut fh = File::open("test.txt")?;
    let mut readcounter = ReadCounter::from(&mut fh);
    let reader = BufReader::new(&mut readcounter);

    for line in reader.lines()
    {
        println!("line: {:?}", line);
    }

    println!("read bytes: {}", readcounter.count());

    Ok(())
}
martin.mein-iserv.de ~/git/rust/bufreader (master) # cargo run      
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/bufreader`
line: Ok("hello")
line: Ok("world")
read bytes: 12

This example might be helpful to others, so I'm posting it here.

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.