Skip to content

Commit

Permalink
do not emit massively complicated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
danielschemmel committed Feb 24, 2024
1 parent cc2da9a commit 7df0c9a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions build-info-proc/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn interpolate_once(
let mut named = None;
if c.is_ascii_digit() {
let mut acc = 0;
#[allow(clippy::blocks_in_if_conditions)]
#[allow(clippy::blocks_in_conditions)]
while {
acc = acc * 10 + c.to_digit(10).unwrap() as usize;
c = chars
Expand All @@ -131,7 +131,7 @@ fn interpolate_once(
explicit_position = Some(acc);
} else if c.is_alphabetic() {
let mut acc = String::new();
#[allow(clippy::blocks_in_if_conditions)]
#[allow(clippy::blocks_in_conditions)]
while {
acc.push(c);
c = chars
Expand Down
2 changes: 2 additions & 0 deletions build-info-proc/src/function/init_value.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Not currently enabled.
use build_info_common::{
chrono::{DateTime, Datelike, NaiveDate, Utc},
semver::Version,
Expand Down
9 changes: 3 additions & 6 deletions build-info-proc/src/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ use proc_macro::TokenStream;
use quote::{quote, quote_spanned};
use syn::{parse, parse_macro_input, Ident, Token, Visibility};

mod init_value;
use init_value::init_value;

struct FunctionSyntax {
definition_crate: Ident,
visibility: Option<Visibility>,
Expand Down Expand Up @@ -34,15 +31,15 @@ pub fn build_info(input: TokenStream, build_info: BuildInfo) -> TokenStream {
} = parse_macro_input!(input as FunctionSyntax);
let visibility = visibility.map_or(quote!(), |vis| quote!(#vis));

let mut tokens = proc_macro2::TokenStream::new();
init_value(&build_info, &mut tokens, &definition_crate);
let bytes = bincode::serialize(&build_info).unwrap();
let bytes = proc_macro2::Literal::byte_string(&bytes);

#[allow(clippy::let_and_return)]
let output = quote_spanned! {
proc_macro::Span::mixed_site().into() =>
#visibility fn #id() -> &'static #definition_crate::BuildInfo {
static VERSION: ::std::sync::OnceLock<#definition_crate::BuildInfo> = ::std::sync::OnceLock::new();
VERSION.get_or_init(|| #tokens)
VERSION.get_or_init(|| #definition_crate::bincode::deserialize(#bytes).unwrap())
}
};

Expand Down
6 changes: 3 additions & 3 deletions build-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ edition = "2021"
rust-version = "1.70.0"

[dependencies]
build-info-common = { version = "=0.0.34", path = "../build-info-common", optional = true }
build-info-common = { version = "=0.0.34", path = "../build-info-common", optional = true, features = ["serde"] }
build-info-proc = { version = "=0.0.34", path = "../build-info-proc" }
bincode = { version = "1", optional = true }

[features]
default = ["runtime"]
oxford-comma = ["build-info-proc/oxford-comma"]
runtime = ["dep:build-info-common", "build-info-proc/runtime"]
serde = ["build-info-common?/serde"]
runtime = ["dep:build-info-common", "dep:bincode", "build-info-proc/runtime"]
5 changes: 5 additions & 0 deletions build-info/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ macro_rules! build_info {
($($tokens:tt)*) => { $crate::proc::build_info!{$crate $($tokens)*} };
}

/// Used by the function generated by `build_info!` to deserialize the build information
#[cfg(feature = "runtime")]
#[doc(hidden)]
pub use bincode;

/**
Generates a string at compile-time that includes build information.
Expand Down

0 comments on commit 7df0c9a

Please sign in to comment.