/* *Thisfileisderivedfromsrc/ffi/c_str.rsintheRuststandardlibrary,used *undertheApacheLicense,Version2.0.Thefollowingistheoriginal *copyrightinformationfromtheRustproject: * *CopyrightsintheRustprojectareretainedbytheircontributors.No *copyrightassignmentisrequiredtocontributetotheRustproject. * *Somefilesincludeexplicitcopyrightnoticesand/orlicensenotices. *Forfullauthorshipinformation,seetheversioncontrolhistoryor *https://thanks.rust-lang.org * *Exceptasotherwisenoted(belowand/orinindividualfiles),Rustis *licensedundertheApacheLicense,Version2.0<LICENSE-APACHE>or *<http://www.apache.org/licenses/LICENSE-2.0> or the MIT license *<LICENSE-MIT>or<http://opensource.org/licenses/MIT>, at your option. * * *LicensedundertheApacheLicense,Version2.0(the"License"); *youmaynotusethisfileexceptincompliancewiththeLicense. *YoumayobtainacopyoftheLicenseat * *http://www.apache.org/licenses/LICENSE-2.0 * *Unlessrequiredbyapplicablelaworagreedtoinwriting,software *distributedundertheLicenseisdistributedonan"ASIS"BASIS, *WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. *SeetheLicenseforthespecificlanguagegoverningpermissionsand *limitationsundertheLicense.
*/
//! Implementation of CString and CStr for use in Trusty. //! //! This module is a lightly modified version of `ffi/c_str.rs` from the Rust //! std crate. `CString::new()` is replaced by the fallible allocating //! [`CString::try_new()`] and other APIs which can allocate infallibly are //! removed.
usecrate::alloc::{AllocError, TryAllocInto}; usecrate::TryClone; use alloc::ffi::CString; use alloc::vec::Vec; use core::slice::memchr;
#[derive(PartialEq, Eq, Debug)] pubenum TryNewError { /// An error indicating that an interior nul byte was found. /// /// While Rust strings may contain nul bytes in the middle, C strings /// can't, as that byte would effectively truncate the string. /// /// This error is created by the [`CString::try_new`] method. /// See its documentation for more. /// /// # Examples /// /// ``` /// use std::ffi::{CString, NulError}; /// /// let _: NulError = CString::new(b"f\0oo".to_vec()).unwrap_err(); /// ```
NulError(usize, Vec<u8>),
pubtrait FallibleCString { /// Creates a new C-compatible string from a container of bytes. /// /// This function will consume the provided data and use the /// underlying bytes to construct a new string, ensuring that /// there is a trailing 0 byte. This trailing 0 byte will be /// appended by this function; the provided data should *not* /// contain any 0 bytes in it. /// /// # Examples /// /// ```ignore (extern-declaration) /// use std::ffi::CString; /// use std::os::raw::c_char; /// /// extern "C" { fn puts(s: *const c_char); } /// /// let to_print = CString::new("Hello!").expect("CString::new failed"); /// unsafe { /// puts(to_print.as_ptr()); /// } /// ``` /// /// # Errors /// /// This function will return an error if the supplied bytes contain an /// internal 0 byte. The [`TryNewError::NulError`] returned will contain the bytes as well as /// the position of the nul byte. fn try_new<T: TryAllocInto<Vec<u8>>>(t: T) -> Result<CString, TryNewError>;
/// Creates a C-compatible string by consuming a byte vector, /// without checking for interior 0 bytes. /// /// This method is equivalent to [`CString::try_new`] except that no runtime /// assertion is made that `v` contains no 0 bytes, and it requires an /// actual byte vector, not anything that can be converted to one with Into. /// /// # Examples /// /// ``` /// use std::ffi::CString; /// /// let raw = b"foo".to_vec(); /// unsafe { /// let c_string = CString::from_vec_unchecked(raw); /// } /// ``` unsafefn try_from_vec_unchecked(v: Vec<u8>) -> Result<CString, AllocError>;
}
impl TryClone for CString { type Error = AllocError;
fn try_clone(&self) -> Result<Self, Self::Error> { let inner = self.as_bytes_with_nul().try_alloc_into()?;
// SAFETY: The `Vec` used here was cloned directly from an existing `CString`, // and so upholds the invariants required.
Ok(unsafe { CString::from_vec_with_nul_unchecked(inner) })
}
}
Messung V0.5 in Prozent
¤ Diese beiden folgenden Angebotsgruppen bietet das Unternehmen0.10Angebot
(Wie Sie bei der Firma Beratungs- und Dienstleistungen beauftragen können 2026-06-27)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.