hollow-0.1.0/.cargo_vcs_info.json0000644000000001360000000000100123360ustar { "git": { "sha1": "00aec01379f3b6d8d2d7eae793a40b376aa3cc9a" }, "path_in_vcs": "" }hollow-0.1.0/.gitignore000064400000000000000000000000101046102023000131050ustar 00000000000000/target hollow-0.1.0/Cargo.toml0000644000000013300000000000100103310ustar # 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] edition = "2021" name = "hollow" version = "0.1.0" authors = ["Blair Noctis "] description = "attr macro to swallow the body of fn attached to" categories = ["development-tools::debugging"] license = "BSD-3-Clause" [lib] proc-macro = true hollow-0.1.0/Cargo.toml.orig000064400000000000000000000004051046102023000140140ustar 00000000000000[package] name = 'hollow' description = 'attr macro to swallow the body of fn attached to' categories = ['development-tools::debugging'] version = '0.1.0' edition = '2021' license = 'BSD-3-Clause' authors = ['Blair Noctis '] [lib] proc-macro = true hollow-0.1.0/LICENSE000064400000000000000000000027261046102023000121420ustar 00000000000000BSD 3-Clause License Copyright (c) Blair Noctis Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. hollow-0.1.0/README.adoc000064400000000000000000000004041046102023000127110ustar 00000000000000= hollow: Rust attr macro that swallows blocks of code https://lib.rs/hollow[image:https://img.shields.io/crates/v/hollow?style=for-the-badge[crates.io version]] https://docs.rs/hollow[image:https://img.shields.io/docsrs/hollow?style=for-the-badge[docs.rs]] hollow-0.1.0/rustfmt.toml000075500000000000000000000002451046102023000135330ustar 00000000000000hard_tabs = true max_width = 120 chain_width = 80 use_small_heuristics = 'Max' reorder_imports = false reorder_modules = false match_block_trailing_comma = true hollow-0.1.0/src/lib.rs000064400000000000000000000043301046102023000130310ustar 00000000000000/*! An easier way to mask code blocks than commenting them out. * * Due to [rust#54727](https://github.com/rust-lang/rust/issues/54727), it can * not be used on `{ }` blocks yet. */ use proc_macro::{Delimiter, Group, TokenStream, TokenTree}; /** Swallow the body of the `fn` it's attached to. * * ```rust * #[hollow::hollow] * fn function_to_swallow() { * panic!("this panic! should be swallowed by hollow"); * } * * function_to_swallow() * ``` * * When its return type does not `impl Default`, or a value other than the * default is desired: * * ```rust * * struct NoDefault { * value: u64, * } * * #[hollow::hollow(value = NoDefault { value: 42 })] * fn custom_return() -> NoDefault { * let a = 4; * let b = 9; * let c = 8; * let d = 1; * NoDefault { value: a * b + c / d } * } * * assert_eq!(42, custom_return().value); */ #[proc_macro_attribute] pub fn hollow(attr: TokenStream, item: TokenStream) -> TokenStream { let body_tokens = if attr.is_empty() { "Default::default()".parse().unwrap() } else { let mut iter = attr.into_iter(); let Some(TokenTree::Ident(next)) = iter.next() else { panic!("invalid attr argument"); }; assert_eq!("value", &next.to_string()); let Some(TokenTree::Punct(next)) = iter.next() else { panic!("invalid attr argument"); }; assert_eq!('=', next.as_char()); TokenStream::from_iter(iter) }; let mut tokens = Vec::new(); /* Items to be hollowed are basically fns; they start with a few Idents, * optionally a <>-delimited generics Group, then a ()- delimited * parameters Group, then an optional Ident set of -> ReturnType, * then a {}-delimited body Group. * * To be hollowed is the body; everything before it should be preserved. * * For fns with a return type, if no attr argument is given, we insert a * `Default::default()` as the body; otherwise, insert the attr argument. */ for token in item.into_iter() { match token { TokenTree::Group(group) => match group.delimiter() { Delimiter::Brace => break, _ => tokens.push(TokenTree::Group(group)), }, other => tokens.push(other), } } tokens.push(TokenTree::Group(Group::new(Delimiter::Brace, body_tokens))); TokenStream::from_iter(tokens) }