about summary refs log tree commit diff
path: root/library/alloc/src/ffi
AgeCommit message (Collapse)AuthorLines
2025-06-24Rollup merge of #137268 - bjoernager:c-string-eq-c-str, r=AmanieuGuillaume Gomez-0/+109
Allow comparisons between `CStr`, `CString`, and `Cow<CStr>`. Closes: #137265 This PR adds the trait implementations proposed in the [ACP](https://github.com/rust-lang/libs-team/issues/517/) under the `c_string_eq_c_str` feature gate: ```rust // core::ffi impl PartialEq<&Self> for CStr; impl PartialEq<CString> for CStr; impl PartialEq<Cow<'_, Self>> for CStr; // alloc::ffi impl PartialEq<CStr> for CString; impl PartialEq<&CStr> for CString; impl PartialEq<Cow<'_, CStr>> for CString; // alloc::borrow impl PartialEq<CStr> for Cow<'_, CStr>; impl PartialEq<&CStr> for Cow<'_, CStr>; impl PartialEq<CString> for Cow<'_, CStr>; ``` As I understand it, stable traits cannot be unstably implemented for stable types, and we would thereby be forced to skip the FCP and directly stabilise these implementations (as is done in this PR). (`@joshtriplett` mentioned that Crater may have to be run).
2025-06-12Delegate `<CStr as Debug>` to `ByteStr`Tamir Duberstein-0/+2
This allows UTF-8 characters to be printed without escapes, rather than just ASCII.
2025-05-12update version placeholdersPietro Albini-1/+1
2025-04-27Rollup merge of #137714 - DiuDiu777:doc-fix, r=tgross35Matthias Krüger-3/+8
Update safety documentation for `CString::from_ptr` and `str::from_boxed_utf8_unchecked` ## PR Description​ This PR addresses missing safety documentation for two APIs: ​**1. alloc::ffi::CStr::from_raw**​ - ​`Alias`: The pointer ​must not be aliased​ (accessed via other pointers) during the reconstructed CString's lifetime. - `Owning`: Calling this function twice on the same pointer and creating two objects with overlapping lifetimes, introduces two alive owners of the same memory. This may result in a double-free. - `Dangling`: The prior documentation required the pointer to originate from CString::into_raw, but this constraint is incomplete. A validly sourced pointer can also cause undefined behavior (UB) if it becomes dangling. A simple Poc for this situation: ``` use std::ffi::CString; use std::os::raw::c_char; fn create_dangling() -> *mut c_char { let local_ptr: *mut c_char = { let valid_data = CString::new("valid").unwrap(); valid_data.into_raw() }; unsafe { let _x = CString::from_raw(local_ptr); } local_ptr } fn main() { let dangling = create_dangling(); unsafe {let _y = CString::from_raw(dangling);} // Cause UB! } ``` ​**2. alloc::str::from_boxed_utf8_unchecked**​ - `ValidStr`: Bytes must contain a ​valid UTF-8 sequence.
2025-04-27Rollup merge of #137439 - clarfonthey:c-str-module, r=tgross35Matthias Krüger-1/+3
Stabilise `std::ffi::c_str` This finished FCP in #112134 but never actually got a stabilisation PR. Since the FCP in #120048 recently passed to add the `os_str` module, it would be nice to also merge this too, to ensure that both get added in the next version. Note: The added stability attributes which *somehow* were able to be omitted before (rustc bug?) were added based on the fact that they were added in 302551388b1942bb4216bb5a15d9d55cee3643a8, which ended up in 1.85.0. Closes: https://github.com/rust-lang/rust/issues/112134 r? libs-api
2025-04-27fix missing doc in CString::from_raw and str::from_boxed_utf8_uncheckedLemonJ-3/+8
2025-04-21Rollup merge of #140118 - tamird:cstr-cleanup, r=joboetChris Denton-3/+3
{B,C}Str: minor cleanup (hopefully) uncontroversial bits extracted from #139994.
2025-04-18Invert `<CString as Deref>::deref` and `CString::as_c_str`Tamir Duberstein-3/+3
This is consistent with the style of `ByteString`.
2025-04-16fix incorrect type in cstr `to_string_lossy()` docsLyndon Brown-1/+1
Restoring what it said prior to commit 67065fe in which it was changed incorrectly with no supporting explanation. Closes #139835.
2025-03-07Fully test the alloc crate through alloctestsbjorn3-11/+5
For the tests that make use of internal implementation details, we include the module to test using #[path] in alloctests now.
2025-02-22Somehow these stability attributes were able to be omitted before?ltdk-0/+2
2025-02-22Stabilise c_str_moduleltdk-1/+1
2025-02-20Implement 'PartialEq<{&Self, CString, Cow<Self>}>' for 'CStr'; Implement ↵Gabriel Bjørnager Jensen-0/+109
'PartialEq<{CStr, &CStr, Cow<CStr>}>' for 'CString'; Implement 'PartialEq<{CStr, &CStr, CString}>' for 'Cow<CStr>';
2025-02-09Mark extern blocks as unsafeMichael Goulet-1/+1
2025-01-26Optimize `Rc::<str>::default()` implementationRené Kijewski-2/+3
This PR lets `impl Default for Rc<str>` re-use the implementation for `Rc::<[u8]>::default()`. The previous version only calculted the memory layout at runtime, even though it should be known at compile time, resulting in an additional function call. The same optimization is done for `Rc<CStr>`. Generated byte code: <https://godbolt.org/z/dfq73jsoP>. Resolves <https://github.com/rust-lang/rust/issues/135784>.
2024-12-28docs: inline `alloc::ffi::c_str` types to `alloc::ffi`Lukas Markeffsky-1/+1
2024-12-04Move some alloc tests to the alloctests cratebjorn3-229/+0
Unit tests directly inside of standard library crates require a very fragile way of building that is hard to reproduce outside of bootstrap.
2024-12-02Use c"lit" for CStrings without unwrapKornel-18/+16
2024-11-27replace placeholder versionBoxy-3/+3
2024-10-29Implement `From<&mut {slice}>` for `Box/Rc/Arc<{slice}>`Eduardo Sánchez Muñoz-0/+31
2024-10-15Rollup merge of #130608 - YohDeadfall:cstr-from-into-str, r=workingjubileeMatthias Krüger-1/+25
Implemented `FromStr` for `CString` and `TryFrom<CString>` for `String` The motivation of this change is making it possible to use `CString` in generic methods with `FromStr` and `TryInto<String>` trait bounds. The same traits are already implemented for `OsString` which is an FFI type too.
2024-10-01Implemented FromStr for CString and TryFrom<CString> for StringYoh Deadfall-1/+25
2024-09-27Mark some more types as having insignificant dtorMichael Goulet-0/+1
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-1/+1
2024-09-19[Clippy] Swap `option_as_ref_deref` to use diagnostic items instead of pathsGnomedDev-0/+1
2024-07-29Reformat `use` declarations.Nicholas Nethercote-18/+13
The previous commit updated `rustfmt.toml` appropriately. This commit is the outcome of running `x fmt --all` with the new formatting options.
2024-06-11replace version placeholderPietro Albini-1/+1
2024-05-12Use shared statics for the ArcInner for Arc<str, CStr>::default, and for ↵Zachary S-13/+0
Arc<[T]>::default where alignof(T) <= 16.
2024-05-12Add note about possible allocation-sharing to Arc/Rc<str/[T]/CStr>::default.Zachary S-0/+4
2024-05-12added Default implsBilly Sheppard-0/+22
reorganised attrs removed OsStr impls added backticks
2024-05-01Describe and use CStr literals in CStr and CString docsGeorge Bateman-9/+5
2024-03-25lib: fix some unnecessary_cast clippy lintklensy-1/+1
warning: casting raw pointers to the same type and constness is unnecessary (`*mut V` -> `*mut V`) --> library\alloc\src\collections\btree\map\entry.rs:357:31 | 357 | let val_ptr = root.borrow_mut().push(self.key, value) as *mut V; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `root.borrow_mut().push (self.key, value)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting to the same type is unnecessary (`usize` -> `usize`) --> library\alloc\src\ffi\c_str.rs:411:56 | 411 | let slice = slice::from_raw_parts_mut(ptr, len as usize); | ^^^^^^^^^^^^ help: try: `len` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting raw pointers to the same type and constness is unnecessary (`*mut T` -> `*mut T`) --> library\alloc\src\slice.rs:516:25 | 516 | (buf.as_mut_ptr() as *mut T).add(buf.len()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `buf.as_mut_ptr()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting raw pointers to the same type and constness is unnecessary (`*mut T` -> `*mut T`) --> library\alloc\src\slice.rs:537:21 | 537 | (buf.as_mut_ptr() as *mut T).add(buf.len()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `buf.as_mut_ptr()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting raw pointers to the same type and constness is unnecessary (`*const ()` -> `*const ()`) --> library\alloc\src\task.rs:151:13 | 151 | waker as *const (), | ^^^^^^^^^^^^^^^^^^ help: try: `waker` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting raw pointers to the same type and constness is unnecessary (`*const ()` -> `*const ()`) --> library\alloc\src\task.rs:323:13 | 323 | waker as *const (), | ^^^^^^^^^^^^^^^^^^ help: try: `waker` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting to the same type is unnecessary (`usize` -> `usize`) --> library\std\src\sys_common\net.rs:110:21 | 110 | assert!(len as usize >= mem::size_of::<c::sockaddr_in>()); | ^^^^^^^^^^^^ help: try: `len` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast warning: casting to the same type is unnecessary (`usize` -> `usize`) --> library\std\src\sys_common\net.rs:116:21 | 116 | assert!(len as usize >= mem::size_of::<c::sockaddr_in6>()); | ^^^^^^^^^^^^ help: try: `len` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_cast
2024-03-10Rollup merge of #112136 - clarfonthey:ffi-c_str, r=cuviperMatthias Krüger-3/+9
Add std::ffi::c_str module ACP: rust-lang/libs-team#134 `std::ffi` docs before change: ![Structs: VaList, VaListImpl, CStr, CString, FromBytesWithNulError, FromVecWithNulError, IntoStringError, NulError, OsStr, OsString](https://github.com/rust-lang/rust/assets/15850505/b2cf3534-30f9-4ef0-a655-bacdc3a19e17) `std::ffi` docs after change: ![Re-exports: self::c_str::{FromBytesWithNulError, FromBytesUntilNulError, FromVecWithNulError, NulError, IntoStringError} ; Modules: c_str ; Structs: VaList, VaListImpl, CStr, CString, OsStr, OsString](https://github.com/rust-lang/rust/assets/15850505/23aa6964-da7a-4942-bbf7-42bde2146f9e) (note: I'm omitting the `c_int`, etc. stuff from the screenshots since it's the same in both. this doesn't just delete those types)
2024-02-22Use generic `NonZero` everywhere in `alloc`.Markus Reiter-9/+9
2024-02-22Add std::ffi::c_str modulesltdk-3/+9
2024-01-28Document From<&CStr> for CStringRyan Lowe-0/+2
2023-12-14Update c_str.rsDaniel Huang-3/+3
2023-12-10remove redundant importssurechen-2/+0
detects redundant imports that can be eliminated. for #117772 : In order to facilitate review and modification, split the checking code and removing redundant imports code into two PR.
2023-07-02fixed documentation of from<CString> for Rc<CStr>: Arc -> RcDaniyar Nurmukhamet-1/+1
2023-02-10Remove a couple of `#[doc(hidden)] pub fn` and their `#[feature]` gatesTobias Bucher-7/+1
2022-09-26remove cfg(bootstrap)Pietro Albini-3/+0
2022-08-22Move error trait into coreJane Losare-Lusby-0/+26
2022-08-21Make some docs nicer wrt pointer offsetsMaybe Waffle-3/+3
2022-07-15Stabilize `core::ffi::CStr`, `alloc::ffi::CString`, and friendsJosh Triplett-7/+7
Stabilize the `core_c_str` and `alloc_c_string` feature gates. Change `std::ffi` to re-export these types rather than creating type aliases, since they now have matching stability.
2022-05-27Finish bumping stage0Mark Rousskov-81/+0
It looks like the last time had left some remaining cfg's -- which made me think that the stage0 bump was actually successful. This brings us to a released 1.62 beta though.
2022-04-14Fix targets not supporting `target_has_atomic = "ptr"`Vadim Petrochenkov-1/+5
2022-04-14library: Use type aliases to make `CStr(ing)` in libcore/liballoc unstableVadim Petrochenkov-6/+6
2022-04-14library: Move `CStr` to libcore, and `CString` to liballocVadim Petrochenkov-0/+1516