Skip to content

Macro syn rewrite #1073

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

Draft
wants to merge 27 commits into
base: rust-next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6b2262f
kbuild: rust: apply `CONFIG_WERROR` to hostprogs as well
ojeda Apr 7, 2024
a5f77c5
kbuild: rust: use shared host Rust flags for `macros`
ojeda Apr 7, 2024
e4527ff
kbuild: rust-analyzer: support key-value `cfg`s
ojeda Apr 10, 2024
c3b22c6
rust: proc-macro2: import crate
ojeda Oct 9, 2022
617c120
rust: proc-macro2: add SPDX License Identifiers
ojeda Oct 9, 2022
24954e2
rust: proc-macro2: remove `unicode_ident` dependency
ojeda Oct 9, 2022
925b30e
rust: quote: import crate
ojeda Oct 9, 2022
cdad907
rust: quote: add SPDX License Identifiers
ojeda Oct 9, 2022
328f151
rust: syn: import crate
ojeda Oct 9, 2022
cef2d41
rust: syn: add SPDX License Identifiers
ojeda Oct 9, 2022
82e9a4f
rust: syn: remove `unicode-ident` dependency
ojeda Oct 9, 2022
a59391f
rust: Kbuild: enable `proc-macro2`, `quote` and `syn`
ojeda Oct 9, 2022
1b729e1
rust: macros: fix soundness issue in `module!` macro
BennoLossin Apr 1, 2024
ed6e1a8
rust: macros: replace `quote!` with `quote::quote` and use `proc-macro2`
BennoLossin Apr 6, 2024
8938c4f
rust: macros: rewrite `#[vtable]` using `syn`
BennoLossin Apr 6, 2024
638dc79
rust: macros: rewrite `module!` using `syn`
BennoLossin Apr 6, 2024
bdb4cff
rust: macros: rewrite `Zeroable` derive macro using `syn`
BennoLossin Apr 6, 2024
2a88e8a
rust: macros: rewrite `#[pin_data]` using `syn`
BennoLossin Apr 6, 2024
b8459ad
rust: macros: rewrite `#[pinned_drop]` using `syn`
BennoLossin Apr 5, 2024
5d4eb2d
rust: macros: rewrite `__internal_init!` using `syn`
BennoLossin Apr 6, 2024
a8dae43
rust: macros: remove helpers
BennoLossin Apr 6, 2024
45d057b
rust: init: remove macros.rs
BennoLossin Apr 8, 2024
c7790b6
fixup! rust: macros: rewrite `#[pin_data]` using `syn`
BennoLossin Apr 16, 2024
66b9b59
fixup! rust: macros: rewrite `#[pinned_drop]` using `syn`
BennoLossin Apr 16, 2024
8d21a65
fixup! rust: macros: rewrite `__internal_init!` using `syn`
BennoLossin Apr 16, 2024
edd1ce0
fixup! rust: macros: rewrite `Zeroable` derive macro using `syn`
BennoLossin Apr 16, 2024
6ad858d
fixup! rust: macros: rewrite `#[vtable]` using `syn`
BennoLossin Apr 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rust: syn: import crate
This is a subset of the Rust `syn` crate,
version 2.0.58, licensed under "Apache-2.0 OR MIT", from:

    https://github.com/dtolnay/syn/raw/2.0.58/src

The files are copied as-is, with no modifications whatsoever
(not even adding the SPDX identifiers).

For copyright details, please see:

    https://github.com/dtolnay/syn/blob/2.0.58/README.md#license
    https://github.com/dtolnay/syn/blob/2.0.58/LICENSE-APACHE
    https://github.com/dtolnay/syn/blob/2.0.58/LICENSE-MIT

The next two patches modify these files as needed for use within
the kernel. This patch split allows reviewers to double-check
the import and to clearly see the differences introduced.

The following script may be used to verify the contents:

    for path in $(cd rust/syn/ && find . -type f -name '*.rs'); do
        curl --silent --show-error --location \
            https://github.com/dtolnay/syn/raw/2.0.58/src/$path \
            | diff --unified rust/syn/$path - && echo $path: OK
    done

Signed-off-by: Miguel Ojeda <[email protected]>
  • Loading branch information
ojeda committed Apr 10, 2024
commit 328f151cbd3ab657ff9707a8b02ac3f68d3ac763
793 changes: 793 additions & 0 deletions rust/syn/attr.rs

Large diffs are not rendered by default.

66 changes: 66 additions & 0 deletions rust/syn/bigint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::ops::{AddAssign, MulAssign};

// For implementing base10_digits() accessor on LitInt.
pub(crate) struct BigInt {
digits: Vec<u8>,
}

impl BigInt {
pub(crate) fn new() -> Self {
BigInt { digits: Vec::new() }
}

pub(crate) fn to_string(&self) -> String {
let mut repr = String::with_capacity(self.digits.len());

let mut has_nonzero = false;
for digit in self.digits.iter().rev() {
has_nonzero |= *digit != 0;
if has_nonzero {
repr.push((*digit + b'0') as char);
}
}

if repr.is_empty() {
repr.push('0');
}

repr
}

fn reserve_two_digits(&mut self) {
let len = self.digits.len();
let desired =
len + !self.digits.ends_with(&[0, 0]) as usize + !self.digits.ends_with(&[0]) as usize;
self.digits.resize(desired, 0);
}
}

impl AddAssign<u8> for BigInt {
// Assumes increment <16.
fn add_assign(&mut self, mut increment: u8) {
self.reserve_two_digits();

let mut i = 0;
while increment > 0 {
let sum = self.digits[i] + increment;
self.digits[i] = sum % 10;
increment = sum / 10;
i += 1;
}
}
}

impl MulAssign<u8> for BigInt {
// Assumes base <=16.
fn mul_assign(&mut self, base: u8) {
self.reserve_two_digits();

let mut carry = 0;
for digit in &mut self.digits {
let prod = *digit * base + carry;
*digit = prod % 10;
carry = prod / 10;
}
}
}
Loading