random-number-macro-impl-0.1.7/.cargo_vcs_info.json0000644000000001660000000000100156500ustar { "git": { "sha1": "7155f30e77c59baef13bd7beae5d02b2bb34a767" }, "path_in_vcs": "random-number-macro-impl" }random-number-macro-impl-0.1.7/Cargo.toml0000644000000022320000000000100136420ustar # 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" rust-version = "1.56" name = "random-number-macro-impl" version = "0.1.7" authors = ["Magic Len "] include = [ "src/**/*", "Cargo.toml", "README.md", "LICENSE", ] description = "Generate random numbers quickly." homepage = "https://magiclen.org/random-number" readme = "README.md" keywords = [ "random", "integer", "number", "inclusive", "fill", ] categories = ["rust-patterns"] license = "MIT" repository = "https://github.com/magiclen/random-number" resolver = "1" [lib] proc_macro = true [dependencies.proc-macro-hack] version = "0.5" [dependencies.quote] version = "1" [dependencies.syn] version = "2" features = ["full"] random-number-macro-impl-0.1.7/Cargo.toml.orig000064400000000000000000000011171046102023000173240ustar 00000000000000[package] name = "random-number-macro-impl" version = "0.1.7" authors = ["Magic Len "] edition = "2021" rust-version = "1.56" repository = "https://github.com/magiclen/random-number" homepage = "https://magiclen.org/random-number" keywords = ["random", "integer", "number", "inclusive", "fill"] categories = ["rust-patterns"] description = "Generate random numbers quickly." license = "MIT" include = ["src/**/*", "Cargo.toml", "README.md", "LICENSE"] [lib] proc_macro = true [dependencies] proc-macro-hack = "0.5" syn = { version = "2", features = ["full"] } quote = "1"random-number-macro-impl-0.1.7/LICENSE000064400000000000000000000020661046102023000154460ustar 00000000000000MIT License Copyright (c) 2020 magiclen.org (Ron Li) 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. 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. random-number-macro-impl-0.1.7/README.md000064400000000000000000000006151046102023000157160ustar 00000000000000Random Number Macro Impl ==================== [![CI](https://github.com/magiclen/random-number/actions/workflows/ci.yml/badge.svg)](https://github.com/magiclen/random-number/actions/workflows/ci.yml) See [`random-number`](https://crates.io/crates/random-number). ## Crates.io https://crates.io/crates/random-number ## Documentation https://docs.rs/random-number ## License [MIT](LICENSE) random-number-macro-impl-0.1.7/src/lib.rs000064400000000000000000000261611046102023000163460ustar 00000000000000use proc_macro::TokenStream; use proc_macro_hack::proc_macro_hack; use quote::quote; use syn::{ parse::{Parse, ParseStream}, parse_macro_input, Expr, RangeLimits, Token, }; struct RandomBuilder { min: Option>, max: Option>, rng: Option>, exclusive: bool, cmp: bool, } impl Parse for RandomBuilder { fn parse(input: ParseStream) -> Result { if input.is_empty() { Ok(RandomBuilder { min: None, max: None, rng: None, exclusive: false, cmp: false, }) } else { let expr: Expr = input.parse()?; if let Expr::Range(range) = expr { let exclusive = match range.limits { RangeLimits::HalfOpen(_) => true, RangeLimits::Closed(_) => false, }; let min = range.start; let max = range.end; if input.is_empty() { Ok(RandomBuilder { min, max, rng: None, exclusive, cmp: false, }) } else { input.parse::()?; let expr: Expr = input.parse()?; Ok(RandomBuilder { min, max, rng: Some(Box::new(expr)), exclusive, cmp: false, }) } } else if input.lookahead1().peek(Token!(,)) { input.parse::()?; let expr2: Expr = input.parse()?; if input.is_empty() { Ok(RandomBuilder { min: Some(Box::from(expr)), max: Some(Box::from(expr2)), rng: None, exclusive: false, cmp: true, }) } else { input.parse::()?; let expr3: Expr = input.parse()?; Ok(RandomBuilder { min: Some(Box::from(expr)), max: Some(Box::from(expr2)), rng: Some(Box::new(expr3)), exclusive: false, cmp: true, }) } } else { Ok(RandomBuilder { min: None, max: None, rng: Some(Box::new(expr)), exclusive: false, cmp: false, }) } } } } #[proc_macro_hack] pub fn random(input: TokenStream) -> TokenStream { let rb = parse_macro_input!(input as RandomBuilder); let random = match rb.min.as_ref() { Some(min) => match rb.max.as_ref() { Some(max) => { if rb.exclusive { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_exclusively_with_rng(#min, #max, &mut #rng) } }, None => { quote! { $crate::random_exclusively(#min, #max) } }, } } else if rb.cmp { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_inclusively_cmp_with_rng(#min, #max, &mut #rng) } }, None => { quote! { $crate::random_inclusively_cmp(#min, #max) } }, } } else { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_inclusively_with_rng(#min, #max, &mut #rng) } }, None => { quote! { $crate::random_inclusively(#min, #max) } }, } } }, None => match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_at_least_with_rng(#min, &mut #rng) } }, None => { quote! { $crate::random_at_least(#min) } }, }, }, None => match rb.max.as_ref() { Some(max) => { if rb.exclusive { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_at_most_exclusively_with_rng(#max, &mut #rng) } }, None => { quote! { $crate::random_at_most_exclusively(#max) } }, } } else { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_at_most_with_rng(#max, &mut #rng) } }, None => { quote! { $crate::random_at_most(#max) } }, } } }, None => match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_with_rng(&mut #rng) } }, None => { quote! { $crate::random() } }, }, }, }; random.into() } // TODO ----------------------------- struct RandomFillBuilder { out: Box, rb: RandomBuilder, } impl Parse for RandomFillBuilder { fn parse(input: ParseStream) -> Result { let expr: Expr = input.parse()?; let out = Box::from(expr); if input.lookahead1().peek(Token!(,)) { input.parse::()?; } let rb: RandomBuilder = input.parse()?; Ok(RandomFillBuilder { out, rb, }) } } #[proc_macro_hack::proc_macro_hack] pub fn random_fill(input: TokenStream) -> TokenStream { let rfb = parse_macro_input!(input as RandomFillBuilder); let out = rfb.out; let rb = rfb.rb; let random_fill = match rb.min.as_ref() { Some(min) => match rb.max.as_ref() { Some(max) => { if rb.exclusive { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_fill_exclusively_with_rng(#out.as_mut(), #min, #max, &mut #rng) } }, None => { quote! { $crate::random_fill_exclusively(#out.as_mut(), #min, #max) } }, } } else if rb.cmp { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_fill_inclusively_cmp_with_rng(#out.as_mut(), #min, #max, &mut #rng) } }, None => { quote! { $crate::random_fill_inclusively_cmp(#out.as_mut(), #min, #max) } }, } } else { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_fill_inclusively_with_rng(#out.as_mut(), #min, #max, &mut #rng) } }, None => { quote! { $crate::random_fill_inclusively(#out.as_mut(), #min, #max) } }, } } }, None => match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_fill_at_least_with_rng(#out.as_mut(), #min, &mut #rng) } }, None => { quote! { $crate::random_fill_at_least(#out.as_mut(), #min) } }, }, }, None => match rb.max.as_ref() { Some(max) => { if rb.exclusive { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_fill_at_most_exclusively_with_rng(#out.as_mut(), #max, &mut #rng) } }, None => { quote! { $crate::random_fill_at_most_exclusively(#out.as_mut(), #max) } }, } } else { match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_fill_at_most_with_rng(#out.as_mut(), #max, &mut #rng) } }, None => { quote! { $crate::random_fill_at_most(#out.as_mut(), #max) } }, } } }, None => match rb.rng.as_ref() { Some(rng) => { quote! { $crate::random_fill_with_rng(#out.as_mut(), &mut #rng) } }, None => { quote! { $crate::random_fill(#out.as_mut()) } }, }, }, }; random_fill.into() }