Skip to content

module!: take strings instead of byte strings #252

Closed

Description

@ojeda

Given it is a proc macro, we could take normal strings for the fields and ensure they are ASCII if/where needed.

This would make the interface a bit leaner.

For author, it would be particularly fitting anyway, because we would like to allow names requiring UTF-8, such as non-romanized names. There are kernel modules with MODULE_AUTHORs with non-ASCII characters already (and encoded as UTF-8) e.g.

MODULE_AUTHOR("周琰杰 (Zhou Yanjie) <zhouyanjie@wanyeetech.com>");
MODULE_AUTHOR("漆鹏振 (Qi Pengzhen) <aric.pzqi@ingenic.com>");

Activity

  1. nbdd0121 commented on May 9, 2021

    @nbdd0121
    Member

    This is actually non-trivial, because binary string constants cannot contain non-ASCII characters, so module proc_macro needs to properly parse the string literal and convert it to a binary string.

  2. bjorn3 commented on May 9, 2021

    @bjorn3
    Member

    str::as_bytes gives the raw UTF-8 bytes of a string. You can directly pass this to Literal::byte_string I believe. Rustc automatically escapes the individual bytes: https://github.com/rust-lang/rust/blob/bba8710616e5e4722215c0d6b27abaedca03ebad/compiler/rustc_expand/src/proc_macro_server.rs#L566-L572

  3. nbdd0121 commented on May 9, 2021

    @nbdd0121
    Member

    str::as_bytes gives the raw UTF-8 bytes of a string. You can directly pass this to Literal::byte_string I believe. Rustc automatically escapes the individual bytes: https://github.com/rust-lang/rust/blob/bba8710616e5e4722215c0d6b27abaedca03ebad/compiler/rustc_expand/src/proc_macro_server.rs#L566-L572

    However there is no way to get a String or Vec<u8> from a Literal other than parsing it yourself. It's easy once once it's parsed. If you look at #258 you'll see that's exactly what I do.

  4. bjorn3 commented on May 9, 2021

    @bjorn3
    Member

    Ah, I see what you mean. If licensing would allow it copying syn or rustc_ast would be the easiest solutions.

  5. nbdd0121 commented on May 12, 2021

    @nbdd0121
    Member

    Are there scenarios that we need to take non-UTF-8 strings?

  6. ojeda commented on May 12, 2021

    @ojeda
    MemberAuthor

    I do not know, but even if we happen to need it, I do not think we should worry about that for the time being, in particular if it makes things more complex.

  7. BennoLossin commented on Jul 25, 2024

    @BennoLossin
    Member

    Stale issue: this is already the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    • libRelated to the `rust/` library.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions