Skip to content

Commit 328f151

Browse files
committed
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]>
1 parent cdad907 commit 328f151

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+46424
-0
lines changed

rust/syn/attr.rs

Lines changed: 793 additions & 0 deletions
Large diffs are not rendered by default.

rust/syn/bigint.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::ops::{AddAssign, MulAssign};
2+
3+
// For implementing base10_digits() accessor on LitInt.
4+
pub(crate) struct BigInt {
5+
digits: Vec<u8>,
6+
}
7+
8+
impl BigInt {
9+
pub(crate) fn new() -> Self {
10+
BigInt { digits: Vec::new() }
11+
}
12+
13+
pub(crate) fn to_string(&self) -> String {
14+
let mut repr = String::with_capacity(self.digits.len());
15+
16+
let mut has_nonzero = false;
17+
for digit in self.digits.iter().rev() {
18+
has_nonzero |= *digit != 0;
19+
if has_nonzero {
20+
repr.push((*digit + b'0') as char);
21+
}
22+
}
23+
24+
if repr.is_empty() {
25+
repr.push('0');
26+
}
27+
28+
repr
29+
}
30+
31+
fn reserve_two_digits(&mut self) {
32+
let len = self.digits.len();
33+
let desired =
34+
len + !self.digits.ends_with(&[0, 0]) as usize + !self.digits.ends_with(&[0]) as usize;
35+
self.digits.resize(desired, 0);
36+
}
37+
}
38+
39+
impl AddAssign<u8> for BigInt {
40+
// Assumes increment <16.
41+
fn add_assign(&mut self, mut increment: u8) {
42+
self.reserve_two_digits();
43+
44+
let mut i = 0;
45+
while increment > 0 {
46+
let sum = self.digits[i] + increment;
47+
self.digits[i] = sum % 10;
48+
increment = sum / 10;
49+
i += 1;
50+
}
51+
}
52+
}
53+
54+
impl MulAssign<u8> for BigInt {
55+
// Assumes base <=16.
56+
fn mul_assign(&mut self, base: u8) {
57+
self.reserve_two_digits();
58+
59+
let mut carry = 0;
60+
for digit in &mut self.digits {
61+
let prod = *digit * base + carry;
62+
*digit = prod % 10;
63+
carry = prod / 10;
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)