fmt2io-0.2.0/.cargo_vcs_info.json0000644000000001120000000000000122240ustar { "git": { "sha1": "08f101b676c6e97b60715bcf02eaee6107c6dd12" } } fmt2io-0.2.0/.gitignore000064400000000000000000000000360000000000000127670ustar 00000000000000/target **/*.rs.bk Cargo.lock fmt2io-0.2.0/Cargo.toml0000644000000016320000000000000102320ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "fmt2io" version = "0.2.0" authors = ["Martin Habovstiak "] description = "A bridge between std::io::Write and std::fmt::Write." homepage = "https://github.com/Kixunil/fmt2io" readme = "README.md" keywords = ["fmt", "io", "Write", "wrapper", "bridge"] categories = ["rust-patterns"] license = "MITNFA" repository = "https://github.com/Kixunil/fmt2io" fmt2io-0.2.0/Cargo.toml.orig000064400000000000000000000006600000000000000136710ustar 00000000000000[package] name = "fmt2io" version = "0.2.0" authors = ["Martin Habovstiak "] description = "A bridge between std::io::Write and std::fmt::Write." homepage = "https://github.com/Kixunil/fmt2io" repository = "https://github.com/Kixunil/fmt2io" readme = "README.md" keywords = ["fmt", "io", "Write", "wrapper", "bridge"] categories = ["rust-patterns"] license = "MITNFA" maintenance = { status = "as-is" } fmt2io-0.2.0/README.md000064400000000000000000000007640000000000000122660ustar 00000000000000Fmt to IO ========= A bridge between `std::io::Write` and `std::fmt::Write`. About ----- Have you ever implemented a nice algorithm that generically uses `fmt::Write` only to find out it doesn't work with `io::Write`? Worry no more - this is the solution! This crate provides a simple `write` function which takes your `io::Write`r, converts it to `fmt::Write`r and provides it to your closure. This way, you can easily bridge the two traits and have truly generic code. License ------- MITNFA fmt2io-0.2.0/src/lib.rs000064400000000000000000000044600000000000000127070ustar 00000000000000//! Have you ever implemented a nice algorithm that generically uses `fmt::Write` //! only to find out it doesn't work with `io::Write`? Worry no more - this is the //! solution! //! //! This crate provides a simple `write` function which takes your `io::Write`r, //! converts it to `fmt::Write`r and provides it to your closure. This way, you //! can easily bridge the two traits and have truly generic code. //! //! Example //! ------- //! //! ```rust //! let mut out = Vec::new(); //! //! use std::fmt::Write; //! //! fmt2io::write(&mut out, |writer| write!(writer, "Hello world!")).unwrap(); //! assert_eq!(out, "Hello world!".as_bytes()); //! ``` use std::{fmt, io}; /// Converts given `io::Write`r to `fmt::Write`r /// /// The `Writer` is constructed from your `writer` which you /// can use to write UTF-8 data to. The function returns underlying io /// Error, if there has been one. /// /// This function uses closure instead of directly exposing `Writer` /// in order to make error handling ergonomic/idiomatic. /// /// ## Panics /// /// This function panics if `writer` didn't return an error but you /// return `Err` from the closure. Return `Ok(Err(YourError))` to handle /// your own error cases. pub fn write(writer: W, f: F) -> io::Result where W: io::Write, F: FnOnce(&mut Writer) -> Result { let mut writer = Writer { writer, result: Ok(()), }; let result = f(&mut writer); writer.result.map(move |_| result.unwrap()) } /// A bridge between `std::io::Write` and `std::fmt::Write`. /// /// This struct provides `fmt::Write` implementation for inner /// writers implementing `io::Write`. It must be used within the /// `write()` function. /// /// See the documentation of `write()` for more information. pub struct Writer { writer: W, result: Result<(), io::Error>, } impl fmt::Write for Writer { fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> { self.writer.write_all(s.as_bytes()).map_err(|err| { self.result = Err(err); fmt::Error }) } } #[cfg(test)] mod tests { #[test] fn it_works() { let mut out = Vec::new(); use std::fmt::Write; ::write(&mut out, |writer| write!(writer, "Hello world!")).unwrap(); assert_eq!(out, "Hello world!".as_bytes()); } }