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

Can this crate work with Associated Constants ? #1

Open
DontBreakAlex opened this issue Jun 27, 2023 · 6 comments
Open

Can this crate work with Associated Constants ? #1

DontBreakAlex opened this issue Jun 27, 2023 · 6 comments

Comments

@DontBreakAlex
Copy link

Currently it produces this error when attempting to use Associated Constants:

error[E0401]: can't use generic parameters from outer function
   --> src/sync.rs:284:47
    |
282 | pub fn get_updated<T: Uploadable>(db: &rusqlite::Connection) -> Result<Vec<T>> {
    |                    - type parameter from outer function
283 |     let mut stmt =
284 |         db.prepare_cached(concat!("SELECT * FROM ", T::SQLITE_TABLE_NAME, " INNER JOIN actions a on ", T::SQLITE_TABLE_NAME, ".uuid = a.u...
    |                                                     ^^^^^^^^^^^^^^^^^^^^ use of generic parameter from outer function

I'm not sure what is going on, can this be supported ?

@rossmacarthur
Copy link
Owner

rossmacarthur commented Jun 28, 2023

I'm not sure this can be supported. But if there is some way to get this working I'd love to add it.

Both generics and const items are evaluated at compile time but I think const items are evaluated before the actual exact type T is known.

Minimal example:

trait Foo {
    const BAR: &'static str;
}

fn takes_foo<T: Foo>() {
    const TEST: &str = T::BAR;
}

@zeenix
Copy link

zeenix commented Dec 22, 2023

@DontBreakAlex I ran into the same. This would save users of my crate (zvariant and zbus in turn) so many allocations as I can then turn one the main/central trait to use associated constants.

@Esper89
Copy link

Esper89 commented Aug 30, 2024

@rossmacarthur This can be fixed by using inline const blocks instead of const items. Inline const blocks can capture generic parameters from their scope and don't require type annotations. This is a recently stabilized feature that requires Rust 1.79.0 or later. This would likely be a MSRV increase for this crate, so you might want to put it behind a feature flag.

@Esper89
Copy link

Esper89 commented Aug 30, 2024

After several attempts to implement this using inline const blocks, I don't think it's actually possible today without basic generic_const_exprs support, because the length of the underlying array would depend on generic parameters.

@zeenix
Copy link

zeenix commented Aug 30, 2024

@DontBreakAlex I ran into the same. This would save users of my crate (zvariant and zbus in turn) so many allocations as I can then turn one the main/central trait to use associated constants.

Just for the record here, I went for a completely different approach and no longer require concatenating strings in const context.

@airblast-dev
Copy link
Contributor

I attempted this when const inlining was first introduced, but was unable to get things to work. I think its mainly due to this section in the RFC: https://rust-lang.github.io/rfcs/2920-inline-const.html#generic-parameters

It also has a pretty good explanation why it isn't supported yet.

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

5 participants