Skip to content

Commit a777cb3

Browse files
committed
Rearrange expr construction in c++ function shim
1 parent f4dc499 commit a777cb3

File tree

1 file changed

+61
-64
lines changed

1 file changed

+61
-64
lines changed

macro/src/expand.rs

+61-64
Original file line numberDiff line numberDiff line change
@@ -650,82 +650,79 @@ fn expand_cxx_function_shim(efn: &ExternFn, types: &Types) -> TokenStream {
650650
}
651651
};
652652
let mut expr;
653-
if efn.throws && efn.ret.is_none() {
654-
expr = call;
655-
} else {
656-
expr = match &efn.ret {
657-
None => call,
658-
Some(ret) => match ret {
659-
Type::Ident(ident) if ident.rust == RustString => {
660-
quote_spanned!(span=> #call.into_string())
661-
}
662-
Type::RustBox(ty) => {
663-
if types.is_considered_improper_ctype(&ty.inner) {
664-
quote_spanned!(span=> ::cxx::alloc::boxed::Box::from_raw(#call.cast()))
665-
} else {
666-
quote_spanned!(span=> ::cxx::alloc::boxed::Box::from_raw(#call))
667-
}
653+
if let Some(ret) = &efn.ret {
654+
expr = match ret {
655+
Type::Ident(ident) if ident.rust == RustString => {
656+
quote_spanned!(span=> #call.into_string())
657+
}
658+
Type::RustBox(ty) => {
659+
if types.is_considered_improper_ctype(&ty.inner) {
660+
quote_spanned!(span=> ::cxx::alloc::boxed::Box::from_raw(#call.cast()))
661+
} else {
662+
quote_spanned!(span=> ::cxx::alloc::boxed::Box::from_raw(#call))
668663
}
669-
Type::RustVec(vec) => {
670-
if vec.inner == RustString {
671-
quote_spanned!(span=> #call.into_vec_string())
672-
} else {
673-
quote_spanned!(span=> #call.into_vec())
674-
}
664+
}
665+
Type::RustVec(vec) => {
666+
if vec.inner == RustString {
667+
quote_spanned!(span=> #call.into_vec_string())
668+
} else {
669+
quote_spanned!(span=> #call.into_vec())
675670
}
676-
Type::UniquePtr(ty) => {
677-
if types.is_considered_improper_ctype(&ty.inner) {
678-
quote_spanned!(span=> ::cxx::UniquePtr::from_raw(#call.cast()))
679-
} else {
680-
quote_spanned!(span=> ::cxx::UniquePtr::from_raw(#call))
681-
}
671+
}
672+
Type::UniquePtr(ty) => {
673+
if types.is_considered_improper_ctype(&ty.inner) {
674+
quote_spanned!(span=> ::cxx::UniquePtr::from_raw(#call.cast()))
675+
} else {
676+
quote_spanned!(span=> ::cxx::UniquePtr::from_raw(#call))
682677
}
683-
Type::Ref(ty) => match &ty.inner {
684-
Type::Ident(ident) if ident.rust == RustString => match ty.mutable {
685-
false => quote_spanned!(span=> #call.as_string()),
686-
true => quote_spanned!(span=> #call.as_mut_string()),
687-
},
688-
Type::RustVec(vec) if vec.inner == RustString => match ty.mutable {
689-
false => quote_spanned!(span=> #call.as_vec_string()),
690-
true => quote_spanned!(span=> #call.as_mut_vec_string()),
691-
},
692-
Type::RustVec(_) => match ty.mutable {
693-
false => quote_spanned!(span=> #call.as_vec()),
694-
true => quote_spanned!(span=> #call.as_mut_vec()),
695-
},
696-
inner if types.is_considered_improper_ctype(inner) => {
697-
let mutability = ty.mutability;
698-
let deref_mut = quote_spanned!(span=> &#mutability *#call.cast());
699-
match ty.pinned {
700-
false => deref_mut,
701-
true => {
702-
quote_spanned!(span=> ::cxx::core::pin::Pin::new_unchecked(#deref_mut))
703-
}
704-
}
705-
}
706-
_ => call,
678+
}
679+
Type::Ref(ty) => match &ty.inner {
680+
Type::Ident(ident) if ident.rust == RustString => match ty.mutable {
681+
false => quote_spanned!(span=> #call.as_string()),
682+
true => quote_spanned!(span=> #call.as_mut_string()),
707683
},
708-
Type::Ptr(ty) => {
709-
if types.is_considered_improper_ctype(&ty.inner) {
710-
quote_spanned!(span=> #call.cast())
711-
} else {
712-
call
713-
}
714-
}
715-
Type::Str(_) => quote_spanned!(span=> #call.as_str()),
716-
Type::SliceRef(slice) => {
717-
let inner = &slice.inner;
718-
match slice.mutable {
719-
false => quote_spanned!(span=> #call.as_slice::<#inner>()),
720-
true => quote_spanned!(span=> #call.as_mut_slice::<#inner>()),
684+
Type::RustVec(vec) if vec.inner == RustString => match ty.mutable {
685+
false => quote_spanned!(span=> #call.as_vec_string()),
686+
true => quote_spanned!(span=> #call.as_mut_vec_string()),
687+
},
688+
Type::RustVec(_) => match ty.mutable {
689+
false => quote_spanned!(span=> #call.as_vec()),
690+
true => quote_spanned!(span=> #call.as_mut_vec()),
691+
},
692+
inner if types.is_considered_improper_ctype(inner) => {
693+
let mutability = ty.mutability;
694+
let deref_mut = quote_spanned!(span=> &#mutability *#call.cast());
695+
match ty.pinned {
696+
false => deref_mut,
697+
true => {
698+
quote_spanned!(span=> ::cxx::core::pin::Pin::new_unchecked(#deref_mut))
699+
}
721700
}
722701
}
723702
_ => call,
724703
},
704+
Type::Ptr(ty) => {
705+
if types.is_considered_improper_ctype(&ty.inner) {
706+
quote_spanned!(span=> #call.cast())
707+
} else {
708+
call
709+
}
710+
}
711+
Type::Str(_) => quote_spanned!(span=> #call.as_str()),
712+
Type::SliceRef(slice) => {
713+
let inner = &slice.inner;
714+
match slice.mutable {
715+
false => quote_spanned!(span=> #call.as_slice::<#inner>()),
716+
true => quote_spanned!(span=> #call.as_mut_slice::<#inner>()),
717+
}
718+
}
719+
_ => call,
725720
};
726721
if efn.throws {
727722
expr = quote_spanned!(span=> ::cxx::core::result::Result::Ok(#expr));
728723
}
724+
} else {
725+
expr = call;
729726
}
730727
let dispatch = quote_spanned!(span=> unsafe { #setup #expr });
731728
let visibility = efn.visibility;

0 commit comments

Comments
 (0)