Skip to content

Conversation

tcanabrava
Copy link
Contributor

I'm new to rust, so please be kind. :)

For routes created with axum::Router - without the axum_extras TypedRoute, I missed the possibility of a compile time check if the route is valid. I really do prefer the axum::Router::new().route().route()...to_owned() approach since it's easy to see all of my routes in one single place.

the change seemed simple but it took a few days of effort untill I discovered I could open a const {} context and work with it.

I do not know how to test this, since the test is compilation failure on the wrong case.

previous api:

axum::Router::new()
   .route("") // runtime error: root path must start with /
   .route("potato") // runtime error: paths must start with /

my proposal:

axum::Router::new()
   .route(vpath!("")) // compile error: root path must start with /
   .route(vpath!("potato")) // compile error: paths must start with /

first I tried my best to not use a macro since they can't be called like impl functions but we work with what we have.
on my (400-ish) route project this saved me a bit of time already.

@tcanabrava tcanabrava force-pushed the tcanabrava/checked_routes branch 2 times, most recently from 6f912ed to 80505f1 Compare March 26, 2025 12:16
@tcanabrava tcanabrava force-pushed the tcanabrava/checked_routes branch 2 times, most recently from f8863e2 to 76485a4 Compare March 26, 2025 13:44
@tcanabrava
Copy link
Contributor Author

aparently the trick I used is only valid on rust 1.80> so I blocked that via a rustversion call.

@tcanabrava
Copy link
Contributor Author

@SabrinaJewson I have fixed all your concerns. can you re-review?

Copy link
Contributor

@SabrinaJewson SabrinaJewson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that I’m not a maintainer of axum! I can just tell you the generic Rust stuff – maintainers might have more overall comments to make.

@tcanabrava tcanabrava force-pushed the tcanabrava/checked_routes branch 2 times, most recently from fe1953f to 9bec4ab Compare March 27, 2025 16:59
@jplatte
Copy link
Member

jplatte commented Mar 27, 2025

This seems like a decent fit for axum-extra. I don't think it's going to be commonly-used enough to warrant being part of the axum API surface, at least initially. Also, let's just not have this at all in Rust <1.80. I think having it exist but do nothing is more problematic than people getting compiler errors because their compiler is too old. In time we may bump the MSRV so this issue disappears entirely.

@tcanabrava
Copy link
Contributor Author

This seems like a decent fit for axum-extra. I don't think it's going to be commonly-used enough to warrant being part of the axum API surface, at least initially. Also, let's just not have this at all in Rust <1.80. I think having it exist but do nothing is more problematic than people getting compiler errors because their compiler is too old. In time we may bump the MSRV so this issue disappears entirely.

no worries at all on my side. I'll move it to axum extras as soon as I'm back home.

@tcanabrava tcanabrava force-pushed the tcanabrava/checked_routes branch from 9bec4ab to 2182e56 Compare March 27, 2025 18:35
Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I took a closer look now. Hope this isn't too annoying to deal with a bunch of rounds of feedback!

One final note, if you could add a changelog entry to axum-extra/CHANGELOG.md that would be nice.

@tcanabrava
Copy link
Contributor Author

Okay, I took a closer look now. Hope this isn't too annoying to deal with a bunch of rounds of feedback!

It's my first time contributing to a rust project, and my first time contributing to axum. so, no, it's not annoying, it's the only way that I can learn. (i'm mostly a c++ dev trying to get away from it).

One final note, if you could add a changelog entry to axum-extra/CHANGELOG.md that would be nice.

Yes, I'll do that.

@tcanabrava
Copy link
Contributor Author

@jplatte did what you asked. but there's a lot of commits that could just be squashed to one. toughts?

Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to do anything about the many commits. I usually squash-merge.

Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jplatte jplatte enabled auto-merge (squash) March 27, 2025 19:52
@jplatte jplatte merged commit ee4727b into tokio-rs:main Mar 27, 2025
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants