-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
feat: add a convenience macro for parsing string literals into &'static Oid
s
#404
Conversation
Thank you for your PR! I think my initial question, is that, is there a reason it's a macro as opposed to a |
so there's few problems fighting against this being a
hopefully this snippet is illustrative of the problems: const fn parse_str(s: &'static str) -> &'static [u32] {
// error[E0435]: attempt to use a non-constant value in a constant
// --> src/main.rs:2:38
// |
// 1 | const fn parse_str(s: &'static str) -> &'static [u32] {
// | - this would need to be a `const`
// 2 | let mut array_for_slice = [0u32; s.len()];
// | ^
let mut array_for_slice = [0u32; s.len()];
// if the above was possible, how to promote `array_for_slice` to a
// `&'static`?
}
// error[E0401]: can't use generic parameters from outer item
// --> src/main.rs:17:30
// |
// 15 | const fn parse_str2<const S: &'static str>() -> &'static [u32] {
// | - const parameter from outer item
// 16 | const ARRAY: &[u32] = const {
// 17 | let mut arr = [0u32; S.len()];
// | ^ use of generic parameter from outer item
// |
// = note: a `const` is a separate item from the item that contains it
//
// error: `&'static str` is forbidden as the type of a const generic parameter
// --> src/main.rs:15:30
// |
// 15 | const fn parse_str2<const S: &'static str>() -> &'static [u32] {
// | ^^^^^^^^^^^^
// |
// = note: the only supported types are integers, `bool`, and `char`
const fn parse_str2<const S: &'static str>() -> &'static [u32] {
const ARRAY: &[u32] = const {
let mut arr = [0u32; S.len()];
// do parsing work here
arr
};
ARRAY
} |
d209603
to
93f1b79
Compare
93f1b79
to
1a61410
Compare
You could get around this by just setting |
I agree, however that does come with the problem of how to actually promote the local value to a |
We can keep it as is then for now. Thank you for your PR! |
this is something I had whipped up for one of my own projects a while back, so figured it might be useful to have in
rasn
proper. there is one caveat with the way this is currently implemented, when an error does occur, the error message is pretty brutal (in that it's filled with a lot of uselessnote
s, but I made an issue on the Rust repo about this, so it should get fixed at some point, as it looks like a regression even: rust-lang/rust#135259).let me know if there's any adjustments or fixes desired for the impl.