From b0f785071111322c1719ac5d2391040216e5b506 Mon Sep 17 00:00:00 2001 From: "Dustin J. Mitchell" Date: Sun, 6 Feb 2022 16:26:09 +0000 Subject: [PATCH] trivially implement PassByValue for usize --- lib/src/atomic.rs | 15 +++++++++++++++ lib/src/lib.rs | 1 + lib/src/string.rs | 5 ++--- 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 lib/src/atomic.rs diff --git a/lib/src/atomic.rs b/lib/src/atomic.rs new file mode 100644 index 000000000..341f7b339 --- /dev/null +++ b/lib/src/atomic.rs @@ -0,0 +1,15 @@ +//! Trait implementations for a few atomic types + +use crate::traits::*; + +impl PassByValue for usize { + type CType = usize; + + unsafe fn from_ctype(arg: usize) -> usize { + arg + } + + fn as_ctype(self) -> usize { + self + } +} diff --git a/lib/src/lib.rs b/lib/src/lib.rs index 18cba4ac9..cd3839f8c 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -4,6 +4,7 @@ mod traits; mod util; pub mod arrays; +pub mod atomic; pub mod replica; pub mod result; pub mod status; diff --git a/lib/src/string.rs b/lib/src/string.rs index 08eed500c..3e95e8f6e 100644 --- a/lib/src/string.rs +++ b/lib/src/string.rs @@ -248,15 +248,14 @@ pub extern "C" fn tc_string_content_with_len( // - lifetime of tcstring outlives the lifetime of this function // - lifetime of tcstring outlives the lifetime of the returned pointer (promised by caller) let tcstring = unsafe { TCString::from_arg_ref(tcstring) }; - debug_assert!(!len_out.is_null()); let bytes = tcstring.as_bytes(); // SAFETY: - // - len_out is not NULL (checked by assertion, promised by caller) + // - len_out is not NULL (promised by caller) // - len_out points to valid memory (promised by caller) // - len_out is properly aligned (C convention) - unsafe { *len_out = bytes.len() }; + unsafe { bytes.len().to_arg_out(len_out) }; bytes.as_ptr() as *const libc::c_char }