macro_tools/attr_prop/
syn_optional.rs

1//!
2//! Property of an attribute which simply wraps one of the standard `syn` types and keeps it optional.
3//!
4use core::marker::PhantomData;
5#[ allow( clippy::wildcard_imports ) ]
6use crate::*;
7// use component_model_types::Assign;
8
9/// Default marker for `AttributePropertyOptionalSyn`.
10/// Used if no marker is defined as parameter.
11#[ derive( Debug, Default, Clone, Copy ) ]
12pub struct AttributePropertyOptionalSynMarker;
13
14///
15/// Property of an attribute which simply wraps one of the standard `syn` types and keeps it optional.
16///
17
18#[ derive( Debug, Clone ) ]
19pub struct AttributePropertyOptionalSyn< T, Marker = AttributePropertyOptionalSynMarker >( Option< T >, ::core::marker::PhantomData< Marker > )
20where
21  T : syn::parse::Parse + quote::ToTokens;
22
23impl< T, Marker > AttributePropertyOptionalSyn< T, Marker >
24where
25  T : syn::parse::Parse + quote::ToTokens,
26{
27  /// Just unwraps and returns the internal data.
28  #[ inline( always ) ]
29  pub fn internal( self ) -> Option< T >
30  {
31    self.0
32  }
33
34  /// Returns an Option reference to the internal data.
35  #[ inline( always ) ]
36  pub fn ref_internal( &self ) -> Option< &T >
37  {
38    self.0.as_ref()
39  }
40}
41
42impl< T, Marker, IntoT > Assign< AttributePropertyOptionalSyn< T, Marker >, IntoT >
43for AttributePropertyOptionalSyn< T, Marker >
44where
45  T : syn::parse::Parse + quote::ToTokens,
46  IntoT : Into< AttributePropertyOptionalSyn< T, Marker > >,
47{
48  /// Inserts value of another instance into the option if it is None, then returns a mutable reference to the contained value.
49  /// If another instance does is None then do nothing.
50  #[ allow( clippy::single_match ) ]
51  #[ inline( always ) ]
52  fn assign( &mut self, component : IntoT )
53  {
54    let component = component.into();
55    match component.0
56    {
57      Some( val ) => { self.0 = Some( val ); },
58      None => {},
59    }
60  }
61}
62
63impl< T, Marker > AttributePropertyComponent for AttributePropertyOptionalSyn< T, Marker >
64where
65  T : syn::parse::Parse + quote::ToTokens,
66  Marker : AttributePropertyComponent,
67{
68  const KEYWORD : &'static str = Marker::KEYWORD;
69}
70
71impl< T, Marker > Default for AttributePropertyOptionalSyn< T, Marker >
72where
73  T : syn::parse::Parse + quote::ToTokens,
74{
75  #[ allow( clippy::default_constructed_unit_structs ) ]
76  fn default() -> Self
77  {
78    Self( None, PhantomData::default() )
79  }
80}
81
82impl< T, Marker > syn::parse::Parse for AttributePropertyOptionalSyn< T, Marker >
83where
84  T : syn::parse::Parse + quote::ToTokens,
85{
86  fn parse( input : syn::parse::ParseStream< '_ > ) -> syn::Result< Self >
87  {
88    input.parse::< syn::Token![ = ] >()?;
89    let value : T = input.parse()?;
90    Ok( value.into() )
91  }
92}
93
94impl< T, Marker > quote::ToTokens for AttributePropertyOptionalSyn< T, Marker >
95where
96  T : syn::parse::Parse + quote::ToTokens,
97{
98  fn to_tokens( &self, tokens : &mut proc_macro2::TokenStream )
99  {
100    self.0.to_tokens( tokens );
101  }
102}
103
104impl< T, Marker > core::ops::Deref for AttributePropertyOptionalSyn< T, Marker >
105where T : syn::parse::Parse + quote::ToTokens
106{
107  type Target = Option< T >;
108  #[ inline( always ) ]
109  fn deref( &self ) -> &Option< T >
110  {
111    &self.0
112  }
113}
114
115impl< T, Marker > AsRef< Option< T > > for AttributePropertyOptionalSyn< T, Marker >
116where T : syn::parse::Parse + quote::ToTokens
117{
118  #[ inline( always ) ]
119  fn as_ref( &self ) -> &Option< T >
120  {
121    &self.0
122  }
123}
124
125impl< T, Marker > From< T > for AttributePropertyOptionalSyn< T, Marker >
126where T : syn::parse::Parse + quote::ToTokens
127{
128  #[ inline( always ) ]
129  #[ allow( clippy::default_constructed_unit_structs ) ]
130  fn from( src : T ) -> Self
131  {
132    Self( Some( src ), PhantomData::default() )
133  }
134}
135
136impl< T, Marker > From< Option< T > > for AttributePropertyOptionalSyn< T, Marker >
137where T : syn::parse::Parse + quote::ToTokens
138{
139  #[ inline( always ) ]
140  #[ allow( clippy::default_constructed_unit_structs ) ]
141  fn from( src : Option< T > ) -> Self
142  {
143    Self( src, PhantomData::default() )
144  }
145}
146
147impl< T, Marker > From< AttributePropertyOptionalSyn< T, Marker > > for Option< T >
148where T : syn::parse::Parse + quote::ToTokens
149{
150  #[ inline( always ) ]
151  fn from( src : AttributePropertyOptionalSyn< T, Marker > ) -> Self
152  {
153    src.0
154  }
155}
156
157impl< 'a, T, Marker > From< &'a AttributePropertyOptionalSyn< T, Marker > > for Option< &'a T >
158where T : syn::parse::Parse + quote::ToTokens
159{
160  #[ inline( always ) ]
161  fn from( src : &'a AttributePropertyOptionalSyn< T, Marker > ) -> Self
162  {
163    src.0.as_ref()
164  }
165}