Create Extension Method with No Parameters to Perform Associated Trait Method #964
-
|
Hello, I've created (but not published) a crate called I am trying to use the Here is the trait: pub trait FromTuple {
type Tuple;
fn from_tuple(tuple: Self::Tuple) -> Self;
}it would look like impl from_nested_tuple::FromTuple for WithNamedFields {
type Tuple = ((char, char), char);
fn from_tuple(tuple: Self::Tuple) -> Self {
let ((a, b), c) = tuple;
Self { a, b, c }
}
}when derived from #[derive(FromTuple)]
struct WithNamedFields {
a: char,
b: char,
c: char,
}If I am not mistaken, the solution would look something like pub trait ParserExt<'src, I, O, E>
where
Self: Parser<'src, I, O, E>,
I: Input<'src>,
E: extra::ParserExtra<'src, I>,
{
fn from_tuple(self) -> ?
where
Self: Sized,
O: FromTuple<Tuple = I>,
{
self.map(FromTuple::from_tuple)
}
}but I am getting confused with all the parameters. Can anyone help me with implementing this trait? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
I think I figured it out (it works, but perhaps my solution is unorthodox). pub trait FromTupleExt<'src, I, O, E>
where
Self: Parser<'src, I, O, E>,
I: Input<'src>,
E: extra::ParserExtra<'src, I>,
{
fn from_tuple<N>(self) -> impl Parser<'src, I, N, E>
where
Self: Sized,
N: FromTuple<Tuple = O>,
{
self.map(FromTuple::from_tuple)
}
}
impl<'src, I, O, E, T> FromTupleExt<'src, I, O, E> for T
where
T: Parser<'src, I, O, E>,
I: Input<'src>,
E: extra::ParserExtra<'src, I>,
{
fn from_tuple<N>(self) -> impl Parser<'src, I, N, E>
where
Self: Sized,
N: FromTuple<Tuple = O>,
{
self.map(FromTuple::from_tuple)
}
} |
Beta Was this translation helpful? Give feedback.
-
|
I was trying to create a recursive parser and then I ran into errors like I tried to add I realized that I could not use my I tried updating the trait signature, and it worked. pub trait FromTupleExt<'src, I, O, E>
where
Self: Parser<'src, I, O, E> + Clone,
I: Input<'src>,
E: extra::ParserExtra<'src, I>,
{
fn from_tuple<N>(self) -> impl Parser<'src, I, N, E> + Clone
where
Self: Sized,
N: FromNestedTuple<Tuple = O>,
{
self.map(FromNestedTuple::from_nested_tuple)
}
}
impl<'src, I, O, E, T> FromTupleExt<'src, I, O, E> for T
where
T: Parser<'src, I, O, E> + Clone,
I: Input<'src>,
E: extra::ParserExtra<'src, I>,
{
fn from_tuple<N>(self) -> impl Parser<'src, I, N, E> + Clone
where
Self: Sized,
N: FromNestedTuple<Tuple = O>,
{
self.map(FromNestedTuple::from_nested_tuple)
}
}However, I don't think it is appropriate for me to force all input parsers to impl |
Beta Was this translation helpful? Give feedback.
Oh interesting, if removing it is a good idea, then perhaps it is worth doing.
I was able to figure out how to satisfy the clone requirement: