Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comple-time checked const contructors for URI types #1626

Closed
SergioBenitez opened this issue Apr 30, 2021 · 1 comment
Closed

Add comple-time checked const contructors for URI types #1626

SergioBenitez opened this issue Apr 30, 2021 · 1 comment

Comments

@SergioBenitez
Copy link
Member

SergioBenitez commented Apr 30, 2021

At present, the only way to construct an Origin or similar directly is through fallible means: parse(). For statically known data, it would be very convenient to have a compile-time checked variant:

impl<'a> Origin<'a> {
    // When used in a `const` context, fails at compile-time when `path` or
    // `query` are invalid `Origin` parts.
    pub const fn new(path: &'a str, query: &'a str) -> Origin<'a> {
        ...
    }
}

We can then provide an origin!() macro that forces a const context:

macro_rules! origin {
    ($path:lit, $query:lit) => ({
        static ORIGIN: Origin<'static = Origin::new($path, $query);
        ORIGIN
    })
}

Alternatively, we can make uri!() smarter so that it allows either Origin or Absolute URIs as the first argument and makes the route part optional. Then, the following would just work:

let _: Absolute<'static> = uri!("http://rocket.rs/foo");
let _: Origin<'static> = uri!("/foo/bar");

let _: Absolute<'static> = uri!("http://rocket.rs/foo", some_route: a, b, c);
let _: Origin<'static> = uri!("/foo/bar", some_route: d, e, f);

cc #998

@SergioBenitez
Copy link
Member Author

Let's just roll this into #998.

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

No branches or pull requests

1 participant