1 unstable release
0.1.0 | Jul 5, 2023 |
---|
#17 in #try-from
6KB
60 lines
A macro to replace function parameters types and convert them to the original type using TryFrom.
The original motivation for this crate was to use it with wasm-bindgen, so a function can .
Example
#[macro_use]
extern crate convert_params;
struct Orig {}
#[derive(Debug)]
struct Foo {}
impl TryFrom<Orig> for Foo {
type Error = &'static str;
fn try_from(_v: Orig) -> Result<Self, Self::Error> {
Ok(Foo {})
}
}
#[derive(Debug, PartialEq)]
struct Error {
s: &'static str,
}
impl From<&'static str> for Error {
fn from(s: &'static str) -> Self {
Error { s }
}
}
#[convert_args(_value1: Orig, _value2: Orig)]
fn example(i: u32, _value1: Foo, _value2: Foo) -> Result<(), Error> {
Ok(())
}
fn main() {
assert!(example(42, Orig {}, Orig {}).is_ok());
}
example
to:
fn example(i: u32, _value1: Orig, _value2: Orig) -> Result<(), Error> {
let _value2 = {
let _value2: Orig = _value2.into();
<Foo as std::convert::TryFrom<_>>::try_from(_value2)?
};
let _value1 = {
let _value1: Orig = _value1.into();
<Foo as std::convert::TryFrom<_>>::try_from(_value1)?
};
Ok(())
}
Dependencies
~1.5MB
~37K SLoC