From 2c71bfe0e14ff037ba0c0487d67df6aab4f74a3d Mon Sep 17 00:00:00 2001 From: maxwase Date: Fri, 19 Aug 2022 23:19:12 +0300 Subject: [PATCH] Update docs, replace libc print macro, fix actual clippy lints --- Cargo.lock | 4 +--- README.md | 6 +++--- ctor/Cargo.toml | 2 +- ctor/src/example.rs | 21 ++++++++++----------- ctor/src/lib.rs | 18 +++++++----------- tests/src/dylib.rs | 11 +++++------ tests/src/dylib_load.rs | 10 +++++----- tests/src/lib.rs | 12 ++++++++---- 8 files changed, 40 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1cc9356..198f289 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,10 +1,8 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 - [[package]] name = "ctor" -version = "0.1.22" +version = "0.1.24" dependencies = [ "libc-print", "quote 1.0.20", diff --git a/README.md b/README.md index faa1b2b..0e06008 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,9 @@ this library explicitly subverts that. The code that runs in the `ctor` and `dtor` functions should be careful to limit itself to `libc` functions and code that does not rely on Rust's stdlib services. -For example, using stdout in a `dtor` function is a guaranteed panic. Consider +For example, using stdout in a `dtor` function before 1.48.0 is a guaranteed panic. Consider using the [`libc-print` crate](https://crates.io/crates/libc-print) for output -to stderr/stdout during `#[ctor]` and `#[dtor]` methods. Other issues +to stderr/stdout during `#[dtor]` methods if you are using an old compiler. Other issues may involve signal processing or panic handling in that early code. In most cases, `sys_common::at_exit` is a better choice than `#[dtor]`. Caveat emptor! @@ -73,7 +73,7 @@ some stdlib services at this time. ```rust #[dtor] unsafe fn shutdown() { - // Using println or eprintln here will panic as Rust has shut down + // Using println or eprintln here will panic as Rust has shut down before Rust 1.48.0 libc::printf("Shutting down!\n\0".as_ptr() as *const i8); } ``` diff --git a/ctor/Cargo.toml b/ctor/Cargo.toml index 18db4ea..50b42da 100644 --- a/ctor/Cargo.toml +++ b/ctor/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ctor" -version = "0.1.23" +version = "0.1.24" authors = ["Matt Mastracci "] edition = "2018" description = "__attribute__((constructor)) for Rust" diff --git a/ctor/src/example.rs b/ctor/src/example.rs index 73babb1..26f71e2 100644 --- a/ctor/src/example.rs +++ b/ctor/src/example.rs @@ -1,8 +1,8 @@ extern crate ctor; extern crate libc_print; -use ctor::*; -use libc_print::*; +use ctor::{ctor, dtor}; +use libc_print::libc_eprintln; use std::collections::HashMap; #[ctor] @@ -12,18 +12,18 @@ static STATIC_CTOR: HashMap = { m.insert(0, "foo"); m.insert(1, "bar"); m.insert(2, "baz"); - libc_eprintln!("STATIC_CTOR"); + eprintln!("STATIC_CTOR"); m }; #[ctor] fn ctor() { - libc_eprintln!("ctor"); + eprintln!("ctor"); } #[ctor] unsafe fn ctor_unsafe() { - libc_eprintln!("ctor_unsafe"); + eprintln!("ctor_unsafe"); } #[dtor] @@ -37,18 +37,17 @@ unsafe fn dtor_unsafe() { } mod module { - use ctor::*; - use libc_print::*; + use ctor::ctor; #[ctor] pub static STATIC_CTOR: u8 = { - libc_eprintln!("module::STATIC_CTOR"); + eprintln!("module::STATIC_CTOR"); 42 }; } pub fn main() { - libc_eprintln!("main!"); - libc_eprintln!("STATIC_CTOR = {:?}", *STATIC_CTOR); - libc_eprintln!("module::STATIC_CTOR = {:?}", *module::STATIC_CTOR); + eprintln!("main!"); + eprintln!("STATIC_CTOR = {:?}", *STATIC_CTOR); + eprintln!("module::STATIC_CTOR = {:?}", *module::STATIC_CTOR); } diff --git a/ctor/src/lib.rs b/ctor/src/lib.rs index 1aad0d5..2e92f0f 100644 --- a/ctor/src/lib.rs +++ b/ctor/src/lib.rs @@ -8,7 +8,7 @@ //! //! This library works and is regularly tested on Linux, OSX and Windows, with both `+crt-static` and `-crt-static`. //! Other platforms are supported but not tested as part of the automatic builds. This library will also work as expected in both -//! `bin` and `cdylib` outputs, ie: the `ctor` and `dtor` will run at executable or library +//! `bin` and `cdylib` outputs, ie: the `ctor` and `dtor` will run at executable or library //! startup/shutdown respectively. //! //! This library currently requires Rust > `1.31.0` at a minimum for the @@ -38,13 +38,9 @@ use proc_macro::TokenStream; /// /// # Examples /// -/// Print a startup message (using `libc_print` for safety): -/// /// ```rust /// # extern crate ctor; -/// # use ctor::*; -/// use libc_print::std_name::println; -/// +/// # use ctor::ctor; /// #[ctor] /// fn foo() { /// println!("Hello, world!"); @@ -59,7 +55,7 @@ use proc_macro::TokenStream; /// /// ```rust /// # extern crate ctor; -/// # use ctor::*; +/// # use ctor::ctor; /// # use std::sync::atomic::{AtomicBool, Ordering}; /// static INITED: AtomicBool = AtomicBool::new(false); /// @@ -74,7 +70,7 @@ use proc_macro::TokenStream; /// ```rust /// # extern crate ctor; /// # use std::collections::HashMap; -/// # use ctor::*; +/// # use ctor::ctor; /// #[ctor] /// static STATIC_CTOR: HashMap = { /// let mut m = HashMap::new(); @@ -188,7 +184,7 @@ pub fn ctor(_attribute: TokenStream, function: TokenStream) -> TokenStream { .. } = var; - if let Some(_) = mutability { + if mutability.is_some() { panic!("#[ctor]-annotated static objects must not be mutable"); } @@ -275,7 +271,7 @@ pub fn ctor(_attribute: TokenStream, function: TokenStream) -> TokenStream { /// /// ```rust /// # extern crate ctor; -/// # use ctor::*; +/// # use ctor::{ctor, dtor}; /// # fn main() {} /// /// #[dtor] @@ -389,7 +385,7 @@ fn validate_item(typ: &str, item: &syn::ItemFn) { } // No parameters allowed - if sig.inputs.len() > 0 { + if !sig.inputs.is_empty() { panic!("#[{}] methods may not have parameters", typ); } diff --git a/tests/src/dylib.rs b/tests/src/dylib.rs index e5c4751..ba818e2 100644 --- a/tests/src/dylib.rs +++ b/tests/src/dylib.rs @@ -1,7 +1,6 @@ #![allow(dead_code, unused_imports)] -use ctor::*; -use libc_print::*; +use ctor::{ctor, dtor}; #[cfg(windows)] extern "C" { @@ -20,7 +19,7 @@ unsafe fn sleep(seconds: u32) { #[ctor] pub static STATIC_INT: u8 = { - libc_ewriteln!("+++ ctor STATIC_INT"); + eprintln!("+++ ctor STATIC_INT"); 200 }; @@ -29,7 +28,7 @@ pub static STATIC_INT: u8 = { #[cfg(target_feature = "crt-static")] unsafe fn ctor() { sleep(1); - libc_ewriteln!("+++ ctor lib (+crt-static)"); + eprintln!("+++ ctor lib (+crt-static)"); } #[ctor] @@ -37,12 +36,12 @@ unsafe fn ctor() { #[cfg(not(target_feature = "crt-static"))] unsafe fn ctor() { sleep(1); - libc_ewriteln!("+++ ctor lib (-crt-static)"); + eprintln!("+++ ctor lib (-crt-static)"); } #[dtor] #[cfg(not(test))] unsafe fn dtor() { sleep(1); - libc_ewriteln!("--- dtor lib"); + eprintln!("--- dtor lib"); } diff --git a/tests/src/dylib_load.rs b/tests/src/dylib_load.rs index ef58cc6..8a14d21 100644 --- a/tests/src/dylib_load.rs +++ b/tests/src/dylib_load.rs @@ -1,14 +1,14 @@ #![allow(unused_imports)] -use ctor::*; +use ctor::{ctor, dtor}; use dlopen::raw::Library; -use libc_print::*; +use libc_print::libc_ewriteln; #[ctor] #[cfg(not(test))] unsafe fn ctor() { sleep(1); - libc_ewriteln!("+ ctor bin"); + eprintln!("+ ctor bin"); } #[dtor] @@ -91,7 +91,7 @@ unsafe fn sleep(seconds: u32) { pub fn main() { unsafe { sleep(1); - libc_ewriteln!("++ main start"); + eprintln!("++ main start"); let lib = Library::open(format!( "target/debug/examples/{}dylib.{}", prefix(), @@ -100,6 +100,6 @@ pub fn main() { .unwrap(); drop(lib); sleep(1); - libc_ewriteln!("-- main end"); + eprintln!("-- main end"); } } diff --git a/tests/src/lib.rs b/tests/src/lib.rs index a281230..1147138 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -5,7 +5,7 @@ extern crate ctor; #[cfg(test)] mod test { - use libc_print::*; + use libc_print::libc_eprintln; use std::path::Path; use std::process::Command; use std::sync::atomic::{AtomicBool, Ordering}; @@ -37,8 +37,8 @@ mod test { #[test] fn test_initialized() { // Test to see that the ctor ran - assert_eq!(true, INITED.load(Ordering::SeqCst)); - assert_eq!(true, INITED_2.load(Ordering::SeqCst)); + assert!(INITED.load(Ordering::SeqCst)); + assert!(INITED_2.load(Ordering::SeqCst)); assert_eq!(*INITED_3, 42); } @@ -88,6 +88,10 @@ mod test { // There are four possible outcomes for stderr, depending on the order // that functions are called - assert!(a == s || b == s || c == s || d == s, "s was unexpected:\n{}", s.replace("\n", "\\n")); + assert!( + a == s || b == s || c == s || d == s, + "s was unexpected:\n{}", + s.replace('\n', "\\n") + ); } }