-
Notifications
You must be signed in to change notification settings - Fork 565
treewide cleanup: drop extern crate, use modern rust
#7501
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
treewide cleanup: drop extern crate, use modern rust
#7501
Conversation
| pub mod regs; | ||
|
|
||
| #[cfg(feature = "tdx")] | ||
| pub mod tdx; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here I also reordered statements a little: first pub modules, then private modules. Instead of mixing modules and use with each other
|
|
||
| use crate::{GuestMemoryMmap, InitramfsConfig, RegionType}; | ||
| mod smbios; | ||
| use std::arch::x86_64; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment above
2d12550 to
d196b2f
Compare
This commit is the first in a series of similar commits to clean up
obsolete `extern crate` statements
Since Rust 1.30, normal macros can be imported via `use`, and with Rust
1.31 and edition 2018 this has become the preferred approach.
`extern crate` is only needed for `alloc` in `no_std` crates, which does
not apply here.
By dropping these (often redundant or odd) `extern crate` lines, we
expose the actual dependencies more clearly and reduce technical debt.
## Auto-generation of the series
Most of this series was produced automatically:
1. Removed all "extern crate" references
2. Run the script [0] to add missing `use` statements
3. Run `cargo +nightly fmt --all`
4. Fix the remaining problems manually
The treewide changes were then split into per-folder commits.
[0]
```python
import os
import re
# Mapping of macro/function usage to imports
MACRO_IMPORTS = {
"info!": "use log::info;\n",
"debug!": "use log::debug;\n",
"error!": "use log::error;\n",
"trace!": "use log::trace;\n",
"warn!": "use log::warn;\n",
"event!": "use event_monitor::event;\n",
"anyhow!(": "use anyhow::anyhow;\n",
"bitflags!(": "use bitflags::bitflags;\n",
"ioctl_ior_nr!": "use vmm_sys_util::{ioctl_ior_nr};\n",
"ioctl_iow_nr!": "use vmm_sys_util::{ioctl_iow_nr};\n",
}
# Regex for finding the first use statement
USE_REGEX = re.compile(r"^\s*(use|pub use) .+?;")
def process_file(path):
with open(path, "r", encoding="utf-8") as f:
lines = f.readlines()
content = "".join(lines)
existing_imports = set(lines)
needed_imports = set()
# Check macros/functions against mapping, only add if not already present
for key, import_stmt in MACRO_IMPORTS.items():
if key in content and import_stmt not in existing_imports:
needed_imports.add(import_stmt)
if not needed_imports:
print(f"Unmodified {path} (no new imports needed)")
return # Nothing to do
# Find first use or pub use statement
for i, line in enumerate(lines):
if USE_REGEX.match(line):
insertion_index = i + 1
break
else:
print(f"Unmodified {path} (no use or pub use statement found)")
return # No use statement found, skip file
# Insert imports
lines[insertion_index:insertion_index] = list(needed_imports)
# Write back file
with open(path, "w", encoding="utf-8") as f:
f.writelines(lines)
print(f"Modified {path}, added imports: {''.join(needed_imports).strip()}")
for root, _, files in os.walk("."):
for file in files:
if file.endswith(".rs"):
process_file(os.path.join(root, file))
```
Signed-off-by: Philipp Schuster <[email protected]>
On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
04c6f88 to
797e5bc
Compare
797e5bc to
01b3bff
Compare
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
This commit is part of a series of similar commits. Signed-off-by: Philipp Schuster <[email protected]> On-behalf-of: SAP [email protected]
01b3bff to
f4edf32
Compare
snue
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I couldn't spot any mistakes in the various transformations. I definitely prefer the clear dependencies. Thanks for taking the time to clean this up.
67fc9d9
This is a series of similar commits to clean up obsolete
extern cratestatementsSince Rust 1.30, normal macros can be imported via
use, and with Rust 1.31 and edition 2018 this has become the preferred approach.extern crateis only needed forallocinno_stdcrates, which does not apply here.By dropping these (often redundant or odd)
extern cratelines, weexpose the actual dependencies more clearly and reduce technical debt.
Part of #7489
I actually did this mostly in September but I just discovered this patch series again. I rebased everything and IMHO it is a clear improvement to code quality.