Recursive definitions are not supported for type aliases

I tried to define a function pointer type like this:

pub type Reader = fn(&str) -> Reader;

It's not supported. It seems that I can't use a trait as an alternative in this scenario either.

So must I use raw pointer and type cast in this scenario ?

How is this type expected to be used? For now, the only thing you can do with Reader is pass into it a string and get another Reader, which doesn't look like it's solving any actual problem.

Reminds me of this.

You can definite it but it doesn't know how to reason about it.

Indirection only gets you so far.

I don't know that I'd recommend resorting to pointers though.

Type erasure works if you give up Fn(&str) -> ?.

1 Like

Every Reader executed then return next Reader for execute,until return std::option::None。

pub type Reader = fn(&str) -> option<Reader>;

Looks like something I want, I'll try this way.

Finally, instead of return the Reader, I use a param carry out the result

It sounds like this should be an Iterator instead.

5 Likes

Actually it's a state machine, this code was written in Java. I'm trying migrate to Rust.

IMO, finite state machines are easiest to write with enums. But it is also possible to write an FSM with async fn at the cost of polling it to completion with an async runtime [1]. The function signature you want to use looks a lot like a parser combinator.


  1. Which could be as simple as pollster. No need to think of async runtimes as somehow inherently complex or heavyweight. ↩︎

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.