Skip to content

Commit 8de9f46

Browse files
committed
feat: v2.0.15
1 parent 3e63dfb commit 8de9f46

File tree

8 files changed

+56
-119
lines changed

8 files changed

+56
-119
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lombok-macros"
3-
version = "2.0.14"
3+
version = "2.0.15"
44
readme = "README.md"
55
edition = "2024"
66
authors = ["root@ltpp.vip"]

src/config/impl.rs

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/config/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub(crate) mod r#impl;
21
pub(crate) mod r#struct;
32

43
pub(crate) use r#struct::*;

src/config/struct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::collections::HashSet;
1414
/// - `param_type_override` - Optional custom parameter type to use instead of deriving from field type.
1515
/// - `skip_flags` - A set of `SkipFlag` indicating which function types should be skipped.
1616
/// - `added_flags` - A set of `AddedFlag` indicating which function types have been added.
17-
#[derive(Debug, Clone)]
17+
#[derive(Clone, Debug, Default)]
1818
pub(crate) struct Config {
1919
/// A `FuncType` that specifies the function type.
2020
pub(crate) func_type: FuncType,

src/func/enum.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,22 @@ pub(crate) enum ParameterType {
5555
/// Represents the return type behavior for getter methods.
5656
///
5757
/// This enum defines how getter methods should return values,
58-
/// controlling whether they return references, cloned values, or use default behavior.
58+
/// controlling whether they return references, cloned values.
5959
///
6060
/// # Variants
6161
/// - `Reference` - Returns a reference to the field value (`&T`).
6262
/// - `Clone` - Returns a cloned copy of the field value (`T`).
6363
/// - `Copy` - Returns a copy of the field value for types implementing Copy trait (`self.field`).
6464
/// - `Deref` - Returns a dereferenced value of the field (`*field`), with match control for Option/Result.
65-
/// - `Default` - Uses default behavior (reference for non-Option/Result, cloned for Option/Result).
66-
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
6766
pub(crate) enum ReturnType {
6867
/// Returns a reference to the field value (`&T`).
68+
#[default]
6969
Reference,
7070
/// Returns a cloned copy of the field value (`T`).
7171
Clone,
7272
/// Returns a copy of the field value for types implementing Copy trait (`self.field`).
7373
Copy,
7474
/// Returns a dereferenced value of the field (`*field`), with match control for Option/Result.
7575
Deref,
76-
/// Uses default behavior (reference for non-Option/Result, cloned for Option/Result).
77-
Default,
7876
}

src/func/impl.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,6 @@ impl FuncType {
116116
}
117117
}
118118

119-
impl Default for ReturnType {
120-
/// Returns the default return type (`Default`).
121-
///
122-
/// # Returns
123-
///
124-
/// - `ReturnType` - The default return type.
125-
#[inline(always)]
126-
fn default() -> Self {
127-
ReturnType::Default
128-
}
129-
}
130-
131119
impl FromStr for ReturnType {
132120
type Err = String;
133121

@@ -148,7 +136,7 @@ impl FromStr for ReturnType {
148136
CLONE => Ok(ReturnType::Clone),
149137
COPY => Ok(ReturnType::Copy),
150138
DEREF => Ok(ReturnType::Deref),
151-
_ => Ok(ReturnType::Default),
139+
_ => Ok(ReturnType::Reference),
152140
}
153141
}
154142
}

src/generate/fn.rs

Lines changed: 45 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -314,18 +314,7 @@ fn generate_assignment_tuple(
314314
fn generate_return_type(field_type: &Type, return_type: ReturnType) -> TokenStream2 {
315315
match return_type {
316316
ReturnType::Reference => {
317-
quote! { &#field_type }
318-
}
319-
ReturnType::Clone | ReturnType::Copy => {
320-
quote! { #field_type }
321-
}
322-
ReturnType::Deref => {
323-
if is_option_type(field_type)
324-
|| is_result_type(field_type)
325-
|| is_arc_type(field_type)
326-
|| is_rc_type(field_type)
327-
|| is_box_type(field_type)
328-
{
317+
if is_option_type(field_type) || is_result_type(field_type) {
329318
if let Type::Path(type_path) = field_type {
330319
if let Some(segment) = type_path.path.segments.last() {
331320
if let PathArguments::AngleBracketed(args) = &segment.arguments {
@@ -344,11 +333,19 @@ fn generate_return_type(field_type: &Type, return_type: ReturnType) -> TokenStre
344333
quote! { #field_type }
345334
}
346335
} else {
347-
quote! { #field_type }
336+
quote! { &#field_type }
348337
}
349338
}
350-
ReturnType::Default => {
351-
if is_option_type(field_type) || is_result_type(field_type) {
339+
ReturnType::Clone | ReturnType::Copy => {
340+
quote! { #field_type }
341+
}
342+
ReturnType::Deref => {
343+
if is_option_type(field_type)
344+
|| is_result_type(field_type)
345+
|| is_arc_type(field_type)
346+
|| is_rc_type(field_type)
347+
|| is_box_type(field_type)
348+
{
352349
if let Type::Path(type_path) = field_type {
353350
if let Some(segment) = type_path.path.segments.last() {
354351
if let PathArguments::AngleBracketed(args) = &segment.arguments {
@@ -367,7 +364,7 @@ fn generate_return_type(field_type: &Type, return_type: ReturnType) -> TokenStre
367364
quote! { #field_type }
368365
}
369366
} else {
370-
quote! { &#field_type }
367+
quote! { #field_type }
371368
}
372369
}
373370
}
@@ -400,12 +397,23 @@ fn build_named_get_quote(
400397
}
401398
let return_ty: TokenStream2 = generate_return_type(attr_ty, return_type);
402399
match return_type {
403-
ReturnType::Reference => quote! {
404-
#[inline(always)]
405-
#vis fn #get_name(&self) -> #return_ty {
406-
&self.#attr_name_ident
400+
ReturnType::Reference => {
401+
if is_option_type(attr_ty) || is_result_type(attr_ty) {
402+
quote! {
403+
#[inline(always)]
404+
#vis fn #get_name(&self) -> #return_ty {
405+
self.#attr_name_ident.clone().unwrap()
406+
}
407+
}
408+
} else {
409+
quote! {
410+
#[inline(always)]
411+
#vis fn #get_name(&self) -> #return_ty {
412+
&self.#attr_name_ident
413+
}
414+
}
407415
}
408-
},
416+
}
409417
ReturnType::Clone => quote! {
410418
#[inline(always)]
411419
#vis fn #get_name(&self) -> #return_ty {
@@ -462,23 +470,6 @@ fn build_named_get_quote(
462470
}
463471
}
464472
}
465-
ReturnType::Default => {
466-
if is_option_type(attr_ty) || is_result_type(attr_ty) {
467-
quote! {
468-
#[inline(always)]
469-
#vis fn #get_name(&self) -> #return_ty {
470-
self.#attr_name_ident.clone().unwrap()
471-
}
472-
}
473-
} else {
474-
quote! {
475-
#[inline(always)]
476-
#vis fn #get_name(&self) -> #return_ty {
477-
&self.#attr_name_ident
478-
}
479-
}
480-
}
481-
}
482473
}
483474
}
484475

@@ -552,12 +543,6 @@ fn build_named_try_get_quote(
552543
quote! {}
553544
}
554545
}
555-
ReturnType::Default => quote! {
556-
#[inline(always)]
557-
#vis fn #try_get_name(&self) -> &#attr_ty {
558-
&self.#attr_name_ident
559-
}
560-
},
561546
}
562547
}
563548

@@ -804,12 +789,23 @@ fn build_tuple_get_quote(
804789
}
805790
let return_ty: TokenStream2 = generate_return_type(attr_ty, return_type);
806791
match return_type {
807-
ReturnType::Reference => quote! {
808-
#[inline(always)]
809-
#vis fn #get_name(&self) -> #return_ty {
810-
&self.#field_index
792+
ReturnType::Reference => {
793+
if is_option_type(attr_ty) || is_result_type(attr_ty) {
794+
quote! {
795+
#[inline(always)]
796+
#vis fn #get_name(&self) -> #return_ty {
797+
self.#field_index.clone().unwrap()
798+
}
799+
}
800+
} else {
801+
quote! {
802+
#[inline(always)]
803+
#vis fn #get_name(&self) -> #return_ty {
804+
&self.#field_index
805+
}
806+
}
811807
}
812-
},
808+
}
813809
ReturnType::Clone => quote! {
814810
#[inline(always)]
815811
#vis fn #get_name(&self) -> #return_ty {
@@ -852,23 +848,6 @@ fn build_tuple_get_quote(
852848
}
853849
}
854850
}
855-
ReturnType::Default => {
856-
if is_option_type(attr_ty) || is_result_type(attr_ty) {
857-
quote! {
858-
#[inline(always)]
859-
#vis fn #get_name(&self) -> #return_ty {
860-
self.#field_index.clone().unwrap()
861-
}
862-
}
863-
} else {
864-
quote! {
865-
#[inline(always)]
866-
#vis fn #get_name(&self) -> #return_ty {
867-
&self.#field_index
868-
}
869-
}
870-
}
871-
}
872851
}
873852
}
874853

@@ -942,12 +921,6 @@ fn build_tuple_try_get_quote(
942921
quote! {}
943922
}
944923
}
945-
ReturnType::Default => quote! {
946-
#[inline(always)]
947-
#vis fn #try_get_name(&self) -> &#attr_ty {
948-
&self.#field_index
949-
}
950-
},
951924
}
952925
}
953926

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) use proc_macro2::{
2323
};
2424
pub(crate) use quote::{ToTokens, format_ident, quote};
2525
pub(crate) use std::{
26-
collections::{HashMap, HashSet},
26+
collections::HashMap,
2727
fmt::{Display, Formatter},
2828
iter::Peekable,
2929
str::FromStr,
@@ -125,10 +125,10 @@ pub(crate) use syn::{
125125
/// result: Ok("success".to_string()),
126126
/// };
127127
/// let optional_value: String = opt_struct.get_optional();
128-
/// let optional_reference: &Option<String> = opt_struct.get_optional_ref();
128+
/// let optional_reference: String = opt_struct.get_optional_ref();
129129
/// let result_value: String = opt_struct.get_result();
130130
/// assert_eq!(optional_value, "value");
131-
/// assert_eq!(*optional_reference, Some("ref_value".to_string()));
131+
/// assert_eq!(optional_reference, "ref_value");
132132
/// assert_eq!(result_value, "success");
133133
/// ```
134134
///
@@ -475,10 +475,10 @@ pub fn setter(input: TokenStream) -> TokenStream {
475475
///
476476
/// let id_reference: &i32 = complex.get_id();
477477
/// let optional_clone: String = complex.get_optional();
478-
/// let result_reference: &Result<i32, String> = complex.get_result();
478+
/// let result_reference: i32 = complex.get_result();
479479
/// assert_eq!(*id_reference, 1);
480480
/// assert_eq!(optional_clone, "value");
481-
/// assert_eq!(*result_reference, Ok(42));
481+
/// assert_eq!(result_reference, 42);
482482
/// ```
483483
///
484484
/// ## Tuple Struct with Combined Accessors

0 commit comments

Comments
 (0)