macro_rules! try_option { ($expr: expr) => { match $expr { Ok(value) => value, Err(err) => { return Some(Err(err.into())); } } }; } use std::mem::ManuallyDrop; use std::ops::{Deref, DerefMut}; pub(crate) use try_option; macro_rules! or { (($($tt: tt)+) else ($($else_tt: tt)*)) => { $($tt)+ }; (() else ($($else_tt: tt)*)) => { $($else_tt)* }; } pub(crate) use or; macro_rules! builder { ( $(#[doc = $doc: literal])* #[builder( name = $builder_name: ident $(, derives = ($($builder_derive: ident),+))? )] $(#[$attr: meta])* $visibility: vis struct $name: ident { $( $(#[$field_attr: meta])* $field_visibility: vis $field: ident: $field_type: ty, )* } ) => { $(#[doc = $doc])* $(#[$attr])* $visibility struct $name { $( $(#[$field_attr])* $field_visibility $field: $field_type, )* } $(#[derive($($builder_derive),+)])? $visibility struct $builder_name { $( $field: $field_type, )* } impl $builder_name { $( #[must_use] $visibility fn $field(mut self, $field: $field_type) -> Self { self.$field = $field; self } )* #[must_use] $visibility fn build(self) -> $name { $name { $( $field: self.$field, )* } } } impl From<$name> for $builder_name { fn from(built: $name) -> Self { Self { $( $field: built.$field, )* } } } }; } pub(crate) use builder; /// Wrapper that ensures the contained value will never be dropped. #[derive(Debug)] pub struct NeverDrop { value: ManuallyDrop, } impl NeverDrop { #[must_use] pub fn new(value: Value) -> Self { Self { value: ManuallyDrop::new(value) } } } impl Deref for NeverDrop { type Target = Value; fn deref(&self) -> &Self::Target { &*self.value } } impl DerefMut for NeverDrop { fn deref_mut(&mut self) -> &mut Self::Target { &mut *self.value } }