fmt2io-1.0.0/.cargo_vcs_info.json0000644000000001360000000000100122320ustar { "git": { "sha1": "bdfcb9f84fe81a74c99d4b429e74411d8c176dca" }, "path_in_vcs": "" }fmt2io-1.0.0/.gitignore000064400000000000000000000000361046102023000130110ustar 00000000000000/target **/*.rs.bk Cargo.lock fmt2io-1.0.0/CHANGELOG.md000064400000000000000000000010371046102023000126340ustar 00000000000000# Changes ## 1.0.0 - Fri Sep 6 2024 * The crate is now considered stable * Support Rust 1.0.0 * Various documentation improvements * Fixed the `maintenance` badge ## 0.2.0 - Tue Jun 22 2021 Breaking change: the `write` function can now return any value in the `Ok` variant. This is breaking because the number of generic arguments changed, however if you didn't use turbofish to specify them there's no real break for you. ## 0.1.0 - Tue Nov 6 2018 Initial version capable of bridging `fmt` to `io` writes that didn't produce any value. fmt2io-1.0.0/Cargo.toml0000644000000017640000000000100102400ustar # 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 are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] name = "fmt2io" version = "1.0.0" authors = ["Martin Habovstiak "] build = false autobins = false autoexamples = false autotests = false autobenches = false description = "A bridge between std::io::Write and std::fmt::Write." readme = "README.md" keywords = [ "fmt", "io", "Write", "wrapper", "bridge", ] categories = ["rust-patterns"] license = "MITNFA" repository = "https://github.com/Kixunil/fmt2io" [lib] name = "fmt2io" path = "src/lib.rs" [badges.maintenance] status = "as-is" fmt2io-1.0.0/Cargo.toml.orig000064400000000000000000000006131046102023000137110ustar 00000000000000[package] name = "fmt2io" version = "1.0.0" authors = ["Martin Habovstiak "] description = "A bridge between std::io::Write and std::fmt::Write." repository = "https://github.com/Kixunil/fmt2io" readme = "README.md" keywords = ["fmt", "io", "Write", "wrapper", "bridge"] categories = ["rust-patterns"] license = "MITNFA" [badges] maintenance = { status = "as-is" } fmt2io-1.0.0/LICENSE000064400000000000000000000030421046102023000120260ustar 00000000000000Copyright (c) 2024 Martin Habovštiak Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Distributions of all or part of the Software intended to be used by the recipients as they would use the unmodified Software, containing modifications that substantially alter, remove, or disable functionality of the Software, outside of the documented configuration mechanisms provided by the Software, shall be modified such that the Original Author's bug reporting email addresses and urls are either replaced with the contact information of the parties responsible for the changes, or removed entirely. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. fmt2io-1.0.0/README.md000064400000000000000000000024101046102023000122760ustar 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. Maintenance status ------------------ Passively maintained/done. The crate does one thing and one thing only. It works as expected and has sensible API. It didn't need to be changed for a very long time and only had one significant code change since its creation. The other changes were purely for cleanup before 1.0 release. It's also very small so there's pretty much zero chance of bugs. Therefore I consider it done. It's not dead, there just doesn't seem to be anything that needs changing. MSRV ---- The minimal supported version of Rust is **1.0.0**. This is not a joke, the crate really doesn't require any fancy compiler features. However I don't object to raising the MSRV up to what's in Debian stable if some really good reason arises. This is highly unlikely to happen. License ------- MITNFA fmt2io-1.0.0/clippy.toml000064400000000000000000000000171046102023000132150ustar 00000000000000msrv = "1.0.0" fmt2io-1.0.0/src/lib.rs000064400000000000000000000064471046102023000127400ustar 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 //! use std::fmt::Write; //! //! let mut out = Vec::new(); //! //! fmt2io::write(&mut out, |writer| write!(writer, "Hello world!"))?; //! assert_eq!(out, "Hello world!".as_bytes()); //! //! # Ok::<(), std::io::Error>(()) //! ``` //! //! Maintenance status //! ------------------ //! //! Passively maintained/done. //! //! The crate does one thing and one thing only. //! It works as expected and has sensible API. //! It didn't need to be changed for a very long time and only had one significant code change since //! its creation. //! The other changes were purely for cleanup before 1.0 release. //! It's also very small so there's pretty much zero chance of bugs. //! Therefore I consider it done. //! It's not dead, there just doesn't seem to be anything that needs changing. //! //! MSRV //! ---- //! //! The minimal supported version of Rust is **1.0.0**. //! This is not a joke, the crate really doesn't require any fancy compiler features. //! However I don't object to raising the MSRV up to what's in Debian stable if some really good //! reason arises. This is highly unlikely to happen. 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. /// /// See the [crate-level documentation](crate) for an example. /// /// ## 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: 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. #[derive(Debug)] 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() { use std::fmt::Write; let mut out = Vec::new(); ::write(&mut out, |writer| write!(writer, "Hello world!")).unwrap(); assert_eq!(out, "Hello world!".as_bytes()); } }