From 673acda92a2e99a81446f912f036c59899054a1e Mon Sep 17 00:00:00 2001 From: van-sprundel Date: Mon, 14 Oct 2024 18:09:52 +0200 Subject: [PATCH] Replace impl `const fn new` with `OnceCell` --- src/system/interface.rs | 5 ----- src/system/timestamp.rs | 25 ++++++++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/system/interface.rs b/src/system/interface.rs index 77ddb4514..aed7cb78c 100644 --- a/src/system/interface.rs +++ b/src/system/interface.rs @@ -30,11 +30,6 @@ impl UserId { pub fn get(&self) -> libc::uid_t { self.0 } - - #[cfg(test)] - pub const fn new_const(uid: libc::uid_t) -> Self { - UserId(uid) - } } impl ProcessId { diff --git a/src/system/timestamp.rs b/src/system/timestamp.rs index a6aea6c87..b9c60a761 100644 --- a/src/system/timestamp.rs +++ b/src/system/timestamp.rs @@ -567,10 +567,13 @@ impl SessionRecord { #[cfg(test)] mod tests { use super::*; - use crate::system::tests::tempfile; + use std::cell::OnceCell; - const TEST_USER_ID: UserId = UserId::new_const(1000); + const TEST_USER_ID: OnceCell = OnceCell::new(); + fn test_user_id() -> UserId { + *TEST_USER_ID.get_or_init(|| UserId::new(1000)) + } #[test] fn can_encode_and_decode() { @@ -688,25 +691,33 @@ mod tests { // valid header should remain valid let c = tempfile_with_data(&[0xD0, 0x50, 0x01, 0x00]).unwrap(); let timeout = Duration::seconds(30); - assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok()); + assert!( + SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).is_ok() + ); let v = data_from_tempfile(c).unwrap(); assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]); // invalid headers should be corrected let c = tempfile_with_data(&[0xAB, 0xBA]).unwrap(); - assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok()); + assert!( + SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).is_ok() + ); let v = data_from_tempfile(c).unwrap(); assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]); // empty header should be filled in let c = tempfile_with_data(&[]).unwrap(); - assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok()); + assert!( + SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).is_ok() + ); let v = data_from_tempfile(c).unwrap(); assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]); // invalid version should reset file let c = tempfile_with_data(&[0xD0, 0x50, 0xAB, 0xBA, 0x0, 0x0]).unwrap(); - assert!(SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).is_ok()); + assert!( + SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).is_ok() + ); let v = data_from_tempfile(c).unwrap(); assert_eq!(&v[..], &[0xD0, 0x50, 0x01, 0x00]); } @@ -716,7 +727,7 @@ mod tests { let timeout = Duration::seconds(30); let c = tempfile_with_data(&[]).unwrap(); let mut srf = - SessionRecordFile::new(TEST_USER_ID, c.try_clone().unwrap(), timeout).unwrap(); + SessionRecordFile::new(test_user_id(), c.try_clone().unwrap(), timeout).unwrap(); let tty_scope = RecordScope::Tty { tty_device: DeviceId::new(0), session_pid: ProcessId::new(0),