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

Probe const_fn #17

Closed
matklad opened this issue Nov 13, 2019 · 3 comments · Fixed by #19
Closed

Probe const_fn #17

matklad opened this issue Nov 13, 2019 · 3 comments · Fixed by #19

Comments

@matklad
Copy link
Collaborator

matklad commented Nov 13, 2019

It would be sweet if it was possible to check not only if a certain path exists, but if it points to a const function. This looks like a significantly more involved check that those currently implemented, so perhaps it should exist just a recipe in the docs? See for an example: https://github.com/seanmonstar/unicase/pull/40/files/889d4761a67f7f61f950e98ab7f8ad5a60001039..39542760d0340195e4c5f14ed914d6ef1a3ca0f6#diff-a7b0a2dee0126cddf994326e705a91eaR12

@cuviper
Copy link
Owner

cuviper commented Nov 13, 2019

I had a couple new methods in mind:

    /// Tests whether the given expression can be used. The type may be `_` to let it be inferred
    /// by the compiler, or even partially inferred like `Result<Vec<_>, _>`.
    ///
    /// The test code is subject to change, but currently looks like:
    ///
    /// ```ignore
    /// pub fn probe() { let _: TYPE = EXPR; }
    /// ```
    pub fn probe_expression(&self, type_: &str, expr: &str) -> bool {
        self.probe(format_args!("pub fn probe() {{ let _: {} = {}; }}", type_, expr))
    }

    /// Tests whether the given constant can be used.
    ///
    /// The test code is subject to change, but currently looks like:
    ///
    /// ```ignore
    /// pub const PROBE: TYPE = INIT;
    /// ```
    pub fn probe_constant(&self, type_: &str, init: &str) -> bool {
        self.probe(format_args!("pub const PROBE: {} = {};", type_, init))
    }

What do you think?

@matklad
Copy link
Collaborator Author

matklad commented Nov 13, 2019

Seems fine to add!

Not sure if type is really necessary though, you can use {{ {}; }} Template to just always force it to unit.

If the user really needs type, they can write the let in block themselves

@cuviper
Copy link
Owner

cuviper commented Nov 13, 2019

In part, I was thinking about avoiding the must_use lint. Otherwise I think your suggestion would be fine for probe_expression, but statements weren't supported at all in constants until rust-lang/rust#57175 -- Rust 1.33.

I think tuples work though, even back to Rust 1.0, something like ((), EXPR).0.

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 a pull request may close this issue.

2 participants