From 1b094952094418a81c9a21ee06903874d224293a Mon Sep 17 00:00:00 2001 From: Sergio Gasquez Arcos Date: Tue, 29 Apr 2025 09:50:59 +0200 Subject: [PATCH] Rust 2024 edition (#497) * build: Bump rust edition and msrv * style: Format code * feat: Add unsafe blocks * feat: Add unsafe blocks for Windows --- .github/workflows/ci.yaml | 2 +- Cargo.toml | 4 ++-- README.md | 2 +- src/cli.rs | 2 +- src/env.rs | 14 +++++++++----- src/error.rs | 6 ++++-- src/host_triple.rs | 2 +- src/lib.rs | 2 +- src/main.rs | 4 ++-- src/targets.rs | 2 +- src/toolchain/gcc.rs | 10 +++++++--- src/toolchain/llvm.rs | 12 ++++++++---- src/toolchain/mod.rs | 8 ++++---- src/toolchain/rust.rs | 32 ++++++++++++++++++++------------ 14 files changed, 62 insertions(+), 40 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 576f878..acec899 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -74,7 +74,7 @@ jobs: - name: Setup Rust toolchain uses: dtolnay/rust-toolchain@stable with: - toolchain: 1.81.0 + toolchain: 1.85.0 - name: Enable caching uses: Swatinem/rust-cache@v2 - name: Cargo check diff --git a/Cargo.toml b/Cargo.toml index a2d3395..88fcd05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "espup" version = "0.15.0" authors = ["Sergio Gasquez Arcos "] -edition = "2021" +edition = "2024" license = "MIT OR Apache-2.0" readme = "README.md" repository = "https://github.com/esp-rs/espup" @@ -11,7 +11,7 @@ Tool for installing and maintaining Espressif Rust ecosystem. """ keywords = ["cli", "embedded", "esp", "esp-rs", "xtensa"] categories = ["command-line-utilities", "development-tools", "embedded"] -rust-version = "1.81.0" +rust-version = "1.85.0" [dependencies] async-trait = "0.1.88" diff --git a/README.md b/README.md index 275bb66..2084cc1 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # espup [![Crates.io](https://img.shields.io/crates/v/espup.svg)](https://crates.io/crates/espup) -![MSRV](https://img.shields.io/badge/MSRV-1.81.0-blue?labelColor=1C2C2E&logo=Rust&style=flat-square) +![MSRV](https://img.shields.io/badge/MSRV-1.85.0-blue?labelColor=1C2C2E&logo=Rust&style=flat-square) [![Continuous Integration](https://github.com/esp-rs/espup/actions/workflows/ci.yaml/badge.svg)](https://github.com/esp-rs/espup/actions/workflows/ci.yaml) [![Security audit](https://github.com/esp-rs/espup/actions/workflows/audit.yaml/badge.svg)](https://github.com/esp-rs/espup/actions/workflows/audit.yaml) [![Matrix](https://img.shields.io/matrix/esp-rs:matrix.org?label=join%20matrix&color=BEC5C9&labelColor=1C2C2E&logo=matrix&style=flat-square)](https://matrix.to/#/#esp-rs:matrix.org) diff --git a/src/cli.rs b/src/cli.rs index 7d1f477..aa1f8dd 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,6 @@ //! Command line interface. -use crate::targets::{parse_targets, Target}; +use crate::targets::{Target, parse_targets}; use clap::Parser; use clap_complete::Shell; use std::{collections::HashSet, path::PathBuf}; diff --git a/src/env.rs b/src/env.rs index d80f470..89d3880 100644 --- a/src/env.rs +++ b/src/env.rs @@ -11,8 +11,8 @@ use std::{ }; #[cfg(windows)] use winreg::{ - enums::{HKEY_CURRENT_USER, KEY_READ, KEY_WRITE}, RegKey, + enums::{HKEY_CURRENT_USER, KEY_READ, KEY_WRITE}, }; #[cfg(windows)] @@ -26,7 +26,7 @@ pub fn set_env_variable(key: &str, value: &str) -> Result<(), Error> { use std::ptr; use winapi::shared::minwindef::*; use winapi::um::winuser::{ - SendMessageTimeoutA, HWND_BROADCAST, SMTO_ABORTIFHUNG, WM_SETTINGCHANGE, + HWND_BROADCAST, SMTO_ABORTIFHUNG, SendMessageTimeoutA, WM_SETTINGCHANGE, }; let hkcu = RegKey::predef(HKEY_CURRENT_USER); @@ -61,7 +61,9 @@ pub fn delete_env_variable(key: &str) -> Result<(), Error> { return Ok(()); } - env::remove_var(key); + unsafe { + env::remove_var(key); + } let hkcu = RegKey::predef(HKEY_CURRENT_USER); let environment_key = hkcu.open_subkey_with_flags("Environment", KEY_READ | KEY_WRITE)?; @@ -164,7 +166,9 @@ pub fn print_post_install_msg(export_file: &Path) -> Result<(), Error> { "\tA file was created at '{}' showing the injected environment variables", export_file.display() ); - println!("\tIf you get still get errors, try manually adding the environment variables by running '{}'", export_file.display() + println!( + "\tIf you get still get errors, try manually adding the environment variables by running '{}'", + export_file.display() ); } #[cfg(unix)] @@ -182,7 +186,7 @@ pub fn print_post_install_msg(export_file: &Path) -> Result<(), Error> { #[cfg(test)] mod tests { - use crate::env::{create_export_file, get_export_file, DEFAULT_EXPORT_FILE}; + use crate::env::{DEFAULT_EXPORT_FILE, create_export_file, get_export_file}; use directories::BaseDirs; use std::{ env::current_dir, diff --git a/src/error.rs b/src/error.rs index ed7ba92..756abc4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,12 +24,14 @@ pub enum Error { #[diagnostic(code(espup::ivalid_destination))] #[error( - "Invalid export file destination: '{0}'. Please, use an absolute or releative path (including the file and its extension)")] + "Invalid export file destination: '{0}'. Please, use an absolute or releative path (including the file and its extension)" + )] InvalidDestination(String), #[diagnostic(code(espup::toolchain::rust::invalid_version))] #[error( - "Invalid toolchain version '{0}'. Verify that the format is correct: '...' or '..', and that the release exists in https://github.com/esp-rs/rust-build/releases")] + "Invalid toolchain version '{0}'. Verify that the format is correct: '...' or '..', and that the release exists in https://github.com/esp-rs/rust-build/releases" + )] InvalidVersion(String), #[error(transparent)] diff --git a/src/host_triple.rs b/src/host_triple.rs index 87ddd51..9944067 100644 --- a/src/host_triple.rs +++ b/src/host_triple.rs @@ -42,7 +42,7 @@ pub fn get_host_triple(host_triple_arg: Option) -> Result Result, Error> { #[cfg(test)] mod tests { - use crate::targets::{parse_targets, Target}; + use crate::targets::{Target, parse_targets}; use std::collections::HashSet; #[test] diff --git a/src/toolchain/gcc.rs b/src/toolchain/gcc.rs index 3de8be6..4fdcd8d 100644 --- a/src/toolchain/gcc.rs +++ b/src/toolchain/gcc.rs @@ -5,7 +5,7 @@ use crate::env::{get_windows_path_var, set_env_variable}; use crate::{ error::Error, host_triple::HostTriple, - toolchain::{download_file, Installable}, + toolchain::{Installable, download_file}, }; use async_trait::async_trait; use log::{debug, info, warn}; @@ -110,9 +110,13 @@ impl Installable for Gcc { &self.get_bin_path() )); if self.arch == RISCV_GCC { - env::set_var("RISCV_GCC", self.get_bin_path()); + unsafe { + env::set_var("RISCV_GCC", self.get_bin_path()); + } } else { - env::set_var("XTENSA_GCC", self.get_bin_path()); + unsafe { + env::set_var("XTENSA_GCC", self.get_bin_path()); + } } } #[cfg(unix)] diff --git a/src/toolchain/llvm.rs b/src/toolchain/llvm.rs index 7108ae6..a44ed4e 100644 --- a/src/toolchain/llvm.rs +++ b/src/toolchain/llvm.rs @@ -5,7 +5,7 @@ use crate::env::{delete_env_variable, get_windows_path_var, set_env_variable}; use crate::{ error::Error, host_triple::HostTriple, - toolchain::{download_file, rust::RE_EXTENDED_SEMANTIC_VERSION, Installable}, + toolchain::{Installable, download_file, rust::RE_EXTENDED_SEMANTIC_VERSION}, }; use async_trait::async_trait; #[cfg(unix)] @@ -335,8 +335,10 @@ impl Installable for Llvm { "$Env:PATH = \"{};\" + $Env:PATH", self.get_lib_path() )); - env::set_var("LIBCLANG_BIN_PATH", self.get_lib_path()); - env::set_var("LIBCLANG_PATH", libclang_dll); + unsafe { + env::set_var("LIBCLANG_BIN_PATH", self.get_lib_path()); + env::set_var("LIBCLANG_PATH", libclang_dll); + } } #[cfg(unix)] if cfg!(unix) { @@ -365,7 +367,9 @@ impl Installable for Llvm { #[cfg(windows)] if cfg!(windows) { exports.push(format!("$Env:CLANG_PATH = \"{}\"", self.get_bin_path())); - env::set_var("CLANG_PATH", self.get_bin_path()); + unsafe { + env::set_var("CLANG_PATH", self.get_bin_path()); + } } #[cfg(unix)] exports.push(format!("export CLANG_PATH=\"{}\"", self.get_bin_path())); diff --git a/src/toolchain/mod.rs b/src/toolchain/mod.rs index 0f2708a..33e3d27 100644 --- a/src/toolchain/mod.rs +++ b/src/toolchain/mod.rs @@ -11,7 +11,7 @@ use crate::{ toolchain::{ gcc::{Gcc, RISCV_GCC, XTENSA_GCC}, llvm::Llvm, - rust::{check_rust_installation, get_rustup_home, RiscVTarget, XtensaRust}, + rust::{RiscVTarget, XtensaRust, check_rust_installation, get_rustup_home}, }, }; use async_trait::async_trait; @@ -22,14 +22,14 @@ use reqwest::{blocking::Client, header}; use retry::{delay::Fixed, retry}; use std::{ env, - fs::{create_dir_all, remove_file, File}, - io::{copy, Write}, + fs::{File, create_dir_all, remove_file}, + io::{Write, copy}, path::{Path, PathBuf}, sync::atomic::{self, AtomicUsize}, }; use tar::Archive; use tokio::{fs::remove_dir_all, sync::mpsc}; -use tokio_retry::{strategy::FixedInterval, Retry}; +use tokio_retry::{Retry, strategy::FixedInterval}; use tokio_stream::StreamExt; use xz2::read::XzDecoder; use zip::ZipArchive; diff --git a/src/toolchain/rust.rs b/src/toolchain/rust.rs index c230845..982a3b1 100644 --- a/src/toolchain/rust.rs +++ b/src/toolchain/rust.rs @@ -4,11 +4,10 @@ use crate::{ error::Error, host_triple::HostTriple, toolchain::{ - download_file, + Installable, download_file, gcc::{RISCV_GCC, XTENSA_GCC}, github_query, llvm::CLANG_NAME, - Installable, }, }; use async_trait::async_trait; @@ -208,10 +207,10 @@ impl Installable for XtensaRust { let output = String::from_utf8_lossy(&rustc_version.stdout); if rustc_version.status.success() && output.contains(&self.version) { warn!( - "Previous installation of Xtensa Rust {} exists in: '{}'. Reusing this installation", - &self.version, - &self.toolchain_destination.display() - ); + "Previous installation of Xtensa Rust {} exists in: '{}'. Reusing this installation", + &self.version, + &self.toolchain_destination.display() + ); return Ok(vec![]); } else { if !rustc_version.status.success() { @@ -359,7 +358,8 @@ impl RiscVTarget { impl Installable for RiscVTarget { async fn install(&self) -> Result, Error> { info!( - "Installing RISC-V Rust targets ('riscv32imc-unknown-none-elf', 'riscv32imac-unknown-none-elf' and 'riscv32imafc-unknown-none-elf') for '{}' toolchain", &self.stable_version + "Installing RISC-V Rust targets ('riscv32imc-unknown-none-elf', 'riscv32imac-unknown-none-elf' and 'riscv32imafc-unknown-none-elf') for '{}' toolchain", + &self.stable_version ); if !Command::new("rustup") @@ -449,7 +449,7 @@ pub async fn check_rust_installation() -> Result<(), Error> { mod tests { use crate::{ logging::initialize_logger, - toolchain::rust::{get_cargo_home, get_rustup_home, XtensaRust}, + toolchain::rust::{XtensaRust, get_cargo_home, get_rustup_home}, }; use directories::BaseDirs; use std::env; @@ -475,7 +475,9 @@ mod tests { #[test] fn test_get_cargo_home() { // No CARGO_HOME set - env::remove_var("CARGO_HOME"); + unsafe { + env::remove_var("CARGO_HOME"); + } assert_eq!( get_cargo_home(), BaseDirs::new().unwrap().home_dir().join(".cargo") @@ -483,14 +485,18 @@ mod tests { // CARGO_HOME set let temp_dir = TempDir::new().unwrap(); let cargo_home = temp_dir.path().to_path_buf(); - env::set_var("CARGO_HOME", cargo_home.to_str().unwrap()); + unsafe { + env::set_var("CARGO_HOME", cargo_home.to_str().unwrap()); + } assert_eq!(get_cargo_home(), cargo_home); } #[test] fn test_get_rustup_home() { // No RUSTUP_HOME set - env::remove_var("RUSTUP_HOME"); + unsafe { + env::remove_var("RUSTUP_HOME"); + } assert_eq!( get_rustup_home(), BaseDirs::new().unwrap().home_dir().join(".rustup") @@ -498,7 +504,9 @@ mod tests { // RUSTUP_HOME set let temp_dir = TempDir::new().unwrap(); let rustup_home = temp_dir.path().to_path_buf(); - env::set_var("RUSTUP_HOME", rustup_home.to_str().unwrap()); + unsafe { + env::set_var("RUSTUP_HOME", rustup_home.to_str().unwrap()); + } assert_eq!(get_rustup_home(), rustup_home); } }