redb_model_derive 0.12.0

Redb model derive macro
Documentation
//! Internal type definitions.
use std::ops::Deref;

use darling::FromMeta;
use quote::format_ident;
use syn::{spanned::Spanned, Lifetime, Type};

/// The `redb` entry `Type` definition. Declares any lifetime as `'static`.
#[derive(Clone)]
pub(crate) struct RedbType(Type);

impl RedbType {
    #[cfg(any(feature = "uuid", feature = "secrecy"))]
    /// Create a new `RedbType` from the given `Type`.
    pub(super) fn new(ty: Type) -> RedbType {
        RedbType(ty)
    }
}

impl Deref for RedbType {
    type Target = Type;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl FromMeta for RedbType {
    fn from_meta(item: &syn::Meta) -> darling::Result<Self> {
        let mut ty = Type::from_meta(item)?;

        // Declare redb lifetimes as 'static`.
        if let Type::Reference(ref mut ref_ty) = ty {
            ref_ty.lifetime = Some(Lifetime {
                apostrophe: ref_ty.span(),
                ident: format_ident!("static"),
            })
        }

        Ok(RedbType(ty))
    }
}