Hello,
I have a syn::Type and would like to check if it is a String
to produce a &str
in my output. I'm getting mad trying to do it. How can I do it? I've been trying to do it for some ours now but haven't managed
Thanks in advance!
Hello,
I have a syn::Type and would like to check if it is a String
to produce a &str
in my output. I'm getting mad trying to do it. How can I do it? I've been trying to do it for some ours now but haven't managed
Thanks in advance!
You can't reliably get type information at macro expansion time. Macros get expanded way before type-check. What exactly are you trying to do?
I've got a struct that has several fields. I want to create a fn
for the struct
that has one parameter for each field. In case its a String
, instead of having a parameter with type &String
I'd like the param type to be &str
.
That's not going to work out. Specializing a single type like that is really hard to do. You can almost do it with specialization
but you still can't project through default associated types and it requires nightly.
Can you just assume that the field is Deref
and output <$ty as std::ops::Deref>::Target
?
I'd like to avoid nigthly if possible. Thanks for the info anyway
On the other hand, I don't understand the Deref
. I'd like to do something like:
#[derive(MyDerive)]
struct A {
field1: String,
field2: u16
}
into:
struct A {
field1: String,
field2: u16
}
impl A {
pub fn my_fn(field1: &str, field2: &u16) {
// some stuff here
}
}
How can I use the Deref
?
You wrote earlier that you want the output to be a &str
but now it looks like you want the function argument to have that type. What are you actually trying to accomplish? Why do you need a &str
argument? Do you want to compare each field with the corresponding function argument without requiring transfer of ownership on the part of the caller? Would it work to just take generic arguments in the function (ie. arg1: &Arg1Type
) and add bounds like Field1Type: Borrow<Arg1Type>
?
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.