diff options
| author | HampusM <hampus@hampusmat.com> | 2023-05-14 12:11:54 +0200 | 
|---|---|---|
| committer | HampusM <hampus@hampusmat.com> | 2023-05-14 12:11:54 +0200 | 
| commit | 9588367284139266b55936d93428355cfa6de906 (patch) | |
| tree | 6a189382758a86be3ac0a396e6d236fe91e65b1a | |
| parent | 1ffc8cbafc4439435307831e87d6f3c265ad819c (diff) | |
feat: add deserializer-static-generics conditional compilation macro
| -rw-r--r-- | src/deserializer/mod.rs | 44 | ||||
| -rw-r--r-- | src/util.rs | 19 | 
2 files changed, 61 insertions, 2 deletions
diff --git a/src/deserializer/mod.rs b/src/deserializer/mod.rs index 918136f..6a512dc 100644 --- a/src/deserializer/mod.rs +++ b/src/deserializer/mod.rs @@ -2,7 +2,7 @@  use std::convert::Infallible;  use crate::tagged::TagStart; -use crate::util::trait_alias; +use crate::util::{feature_alternate, trait_alias};  use crate::DeserializeTagged;  pub mod buffered; @@ -278,3 +278,45 @@ macro_rules! impl_from_deserializer_error {          }      };  } + +feature_alternate!( +    feature = "deserializer-static-generics", +    /// Conditional compilation based on whether or not the `deserializer-static-generics` +    /// feature is enabled. +    /// +    /// # Examples +    /// ``` +    /// use std::io::Cursor; +    /// +    /// use xml_stinks::xml_stinks_if_deserializer_static_generics; +    /// use xml_stinks::deserializer::buffered::Buffered as BufferedDeserializer; +    /// use xml_stinks::deserializer::Deserializer; +    /// +    /// fn do_something(bytes: &[u8]) +    /// { +    ///     let deserializer = xml_stinks_if_deserializer_static_generics!(then { +    ///         BufferedDeserializer::new(Cursor::new(bytes.to_vec())); +    ///     } else { +    ///         // This wouldn't compile if the deserializer-static-generics feature was +    ///         // enabled +    ///         BufferedDeserializer::new(bytes); +    ///     }); +    /// +    ///     // ... +    /// } +    /// ``` +    when_enabled = +        #[macro_export] +        macro_rules! xml_stinks_if_deserializer_static_generics { +            (then { $($then: tt)* }$(else { $($else: tt)* })?) => { +                $($then)* +            }; +        }, +    when_disabled = +        #[macro_export] +        macro_rules! xml_stinks_if_deserializer_static_generics { +            (then { $($then: tt)* }$(else { $($else: tt)* })?) => { +                $($($else)*)? +            }; +        } +); diff --git a/src/util.rs b/src/util.rs index 78d2cd4..5c9ffb8 100644 --- a/src/util.rs +++ b/src/util.rs @@ -10,4 +10,21 @@ macro_rules! trait_alias {      };  } -pub(crate) use trait_alias; +macro_rules! feature_alternate { +    ( +        feature = $feature: literal, +        $(#[doc = $doc: literal])* +        when_enabled = $when_enabled: item, +        when_disabled = $when_disabled: item +    ) => { +        $(#[doc = $doc])* +        #[cfg(feature = $feature)] +        $when_enabled + +        $(#[doc = $doc])* +        #[cfg(not(feature = $feature))] +        $when_disabled +    }; +} + +pub(crate) use {feature_alternate, trait_alias};  | 
