Support more unusual reference types in Outgoing derive
This commit is contained in:
parent
16dd20d64e
commit
3853a36ff6
@ -240,37 +240,43 @@ fn strip_lifetimes(field_type: &mut Type) -> bool {
|
|||||||
|
|
||||||
has_lifetimes || is_lifetime_generic
|
has_lifetimes || is_lifetime_generic
|
||||||
}
|
}
|
||||||
Type::Reference(TypeReference { elem, .. }) => match &mut **elem {
|
Type::Reference(TypeReference { elem, .. }) => {
|
||||||
Type::Path(ty_path) => {
|
let special_replacement = match &mut **elem {
|
||||||
let TypePath { path, .. } = ty_path;
|
Type::Path(ty) => {
|
||||||
let segs = path
|
let path = &ty.path;
|
||||||
.segments
|
let last_seg = path.segments.last().unwrap();
|
||||||
.clone()
|
|
||||||
.into_iter()
|
|
||||||
.map(|seg| seg.ident.to_string())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
if path.is_ident("str") {
|
if last_seg.ident == "str" {
|
||||||
// &str -> String
|
// &str -> String
|
||||||
*field_type = parse_quote! { ::std::string::String };
|
Some(parse_quote! { ::std::string::String })
|
||||||
} else if segs.contains(&"DeviceId".into()) || segs.contains(&"ServerName".into()) {
|
} else if last_seg.ident == "DeviceId" || last_seg.ident == "ServerName" {
|
||||||
// The identifiers that need to be boxed `Box<T>` since they are DST's.
|
// The identifiers that need to be boxed `Box<T>` since they are DST's.
|
||||||
*field_type = parse_quote! { ::std::boxed::Box<#path> };
|
Some(parse_quote! { ::std::boxed::Box<#path> })
|
||||||
} else {
|
} else {
|
||||||
// &T -> T
|
None
|
||||||
*field_type = Type::Path(ty_path.clone());
|
}
|
||||||
}
|
}
|
||||||
true
|
// &[T] -> Vec<T>
|
||||||
}
|
Type::Slice(TypeSlice { elem, .. }) => {
|
||||||
// &[T] -> Vec<T>
|
// Recursively strip the lifetimes of the slice's elements.
|
||||||
Type::Slice(TypeSlice { elem, .. }) => {
|
strip_lifetimes(&mut *elem);
|
||||||
// Recursively strip the lifetimes of the slice's elements.
|
Some(parse_quote! { Vec<#elem> })
|
||||||
strip_lifetimes(&mut *elem);
|
}
|
||||||
*field_type = parse_quote! { Vec<#elem> };
|
_ => None,
|
||||||
true
|
};
|
||||||
}
|
|
||||||
_ => false,
|
*field_type = match special_replacement {
|
||||||
},
|
Some(ty) => ty,
|
||||||
|
None => {
|
||||||
|
// Strip lifetimes of `elem`.
|
||||||
|
strip_lifetimes(elem);
|
||||||
|
// Replace reference with `elem`.
|
||||||
|
(**elem).clone()
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
true
|
||||||
|
}
|
||||||
Type::Tuple(syn::TypeTuple { elems, .. }) => {
|
Type::Tuple(syn::TypeTuple { elems, .. }) => {
|
||||||
let mut has_lifetime = false;
|
let mut has_lifetime = false;
|
||||||
for elem in elems {
|
for elem in elems {
|
||||||
|
@ -32,6 +32,9 @@ pub struct FakeRequest<'a, T> {
|
|||||||
pub option: Option<&'a [u8]>,
|
pub option: Option<&'a [u8]>,
|
||||||
pub depth: Option<&'a [(&'a str, &'a str)]>,
|
pub depth: Option<&'a [(&'a str, &'a str)]>,
|
||||||
pub arc_type: std::sync::Arc<&'a ::ruma_identifiers::ServerName>,
|
pub arc_type: std::sync::Arc<&'a ::ruma_identifiers::ServerName>,
|
||||||
|
pub thing_ref: &'a Thing<'a, T>,
|
||||||
|
pub double_ref: &'a &'a u8,
|
||||||
|
pub triple_ref: &'a &'a &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Outgoing)]
|
#[derive(Outgoing)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user