about summary refs log tree commit diff
path: root/library/core/src/ffi
AgeCommit message (Collapse)AuthorLines
2025-09-27Library: Remove remaining private `#[repr]` workaroundsLeón Orell Valerian Liehr-5/+5
Co-authored-by: David Tolnay <dtolnay@gmail.com>
2025-09-24core: simplify `CStr::default()`joboet-3/+1
Just use a `CStr`-literal...
2025-09-19Fix std build for xtensaThalia Archibald-0/+1
2025-09-13c-variadic: document `core::ffi::VaArgSafe`Folkert de Vries-6/+23
and document `VaList::arg`.
2025-09-01Constify conversion traitsltdk-1/+2
2025-08-06replace version placeholderBoxy-1/+1
2025-07-17Rollup merge of #143326 - hkBst:rm-deprecated-1, r=jhprattMatthias Krüger-17/+9
Remove deprecated `Error::description` impl from `c_str::FromBytesWithNulError`
2025-07-05move the `va_copy`, `va_arg` and `va_end` to `core::intrinsics`Folkert de Vries-17/+1
2025-07-02remove deprecated from core::ffi::c_strMarijn Schouten-17/+9
2025-07-02byte-addresses memory -> byte-addressed memoryMarijn Schouten-1/+1
2025-06-24Corrected spelling mistake in c_str.rsMetaNova-1/+1
Changed "you're" to "your" on line 470.
2025-06-24Rollup merge of #137268 - bjoernager:c-string-eq-c-str, r=AmanieuGuillaume Gomez-0/+14
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-1/+3
This allows UTF-8 characters to be printed without escapes, rather than just ASCII.
2025-05-31terminology: allocated object → allocationRalf Jung-1/+1
2025-05-29Rollup merge of #139994 - tamird:cstr-display, r=AmanieuJacob Pratt-0/+24
add `CStr::display` The implementation delegates to `<ByteStr as Display>::fmt`. Link: https://github.com/rust-lang/libs-team/issues/550 Link: https://github.com/rust-lang/rust/issues/139984. r? ```@BurntSushi``` cc ```@Darksonn``` ```@tgross35``` ```@ojeda``` ```@joshtriplett```
2025-05-25Rollup merge of #141361 - folkertdev:varargs-cfg, r=workingjubileeJacob Pratt-175/+136
use `cfg_select!` to select the right `VaListImpl` definition tracking issue: https://github.com/rust-lang/rust/issues/44930 Just a bit of cleanup really. We could use `PhantomInvariantLifetime<'f>` (https://github.com/rust-lang/rust/issues/135806) to make it more precise what that `PhantomData<&'f mut &'f c_void>` marker is doing. I'm not sure how ready that feature is though, `@jhpratt` are these types good to use internally? --- Some research into the lifetimes of `VaList` and `VaListImpl`: It's easy to see why the lifetime of these types should not be extended, a `VaList` or `VaListImpl` escaping its function is a bad idea. I don't currently see why coercing the lifetime to a shorter lifetime is problematic though, but probably I just don't understand variance well enough to see it. The history does not provide much explanation: - https://github.com/immunant/rust/commit/08140878fefaa4b16939b904bf825b7107069b42 original implementation - https://github.com/immunant/rust/commit/b9ea653aee231114acbe6d4b3c7b1d692772d060 adds `VaListImpl<'f>`, but it is only covariant in `'f` - https://github.com/rust-lang/rust/pull/62639 makes `VaListImpl<'f>` invariant over `'f` (because `VaList<'a, 'f>` is already invariant over `'f`, but I think that is just an implementation detail?) Beyond that I don't see how the lifetime situation can be simplified significantly, e.g. this function really needs `'copy` to be unconstrained. ```rust /// Copies the `va_list` at the current location. pub unsafe fn with_copy<F, R>(&self, f: F) -> R where F: for<'copy> FnOnce(VaList<'copy, 'f>) -> R, { let mut ap = self.clone(); let ret = f(ap.as_va_list()); // SAFETY: the caller must uphold the safety contract for `va_end`. unsafe { va_end(&mut ap); } ret } ``` `@rustbot` label +F-c_variadic r? `@workingjubilee`
2025-05-24Use C-string literals to reduce boilerplateTamir Duberstein-34/+7
Reduce boilerplate in doctests by replacing fallible function calls with literals.
2025-05-24add CStr::displayTamir Duberstein-0/+24
The implementation delegates to `<ByteStr as Display>::fmt`. Link: https://github.com/rust-lang/libs-team/issues/550 Link: https://github.com/rust-lang/rust/issues/139984.
2025-05-23use `cfg_select!` to select the right `VaListImpl` definitionFolkert de Vries-175/+136
2025-05-22Auto merge of #137198 - tgross35:cfg-match-rename, r=Amanieubors-3/+3
Rename `cfg_match!` to `cfg_select!` [`@Nemo157` pointed out](https://github.com/rust-lang/rust/issues/115585#issuecomment-2346307605) that `cfg_match!` syntax does not actually align well with match syntax, which is a possible source of confusion. The comment points out that usage is instead more similar to ecosystem `select!` macros. Rename `cfg_match!` to `cfg_select!` to match this. Tracking issue: https://github.com/rust-lang/rust/issues/115585 [1]: https://github.com/rust-lang/rust/issues/115585#issuecomment-2346307605
2025-05-21limit impls of `VaArgSafe` to just types that are actually safeFolkert de Vries-27/+45
8 and 16-bit integers are subject to upcasting in C, and hence are not reliably safe. users should perform their own casting and deal with the consequences
2025-05-20Rename `cfg_match!` to `cfg_select!`Trevor Gross-3/+3
At [1] it was pointed out that `cfg_match!` syntax does not actually align well with match syntax, which is a possible source of confusion. The comment points out that usage is instead more similar to ecosystem `select!` macros. Rename `cfg_match!` to `cfg_select!` to match this. Tracking issue: https://github.com/rust-lang/rust/issues/115585 [1]: https://github.com/rust-lang/rust/issues/115585#issuecomment-2346307605
2025-05-12update version placeholdersPietro Albini-1/+1
2025-04-27Rollup merge of #140297 - shepmaster:cstr-lossy, r=joboetMatthias Krüger-2/+3
Update example to use CStr::to_string_lossy
2025-04-27Rollup merge of #137439 - clarfonthey:c-str-module, r=tgross35Matthias Krüger-1/+1
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-26Update example to use `CStr::to_string_lossy`Jake Goulding-2/+3
2025-04-18Remove errant doc comment linesTamir Duberstein-1/+0
2025-03-26Use cfg_match in coreChristopher Durham-12/+16
2025-03-12intrinsics: remove unnecessary leading underscore from argument namesRalf Jung-3/+3
2025-03-10Target definition for `wasm32-wali-linux-musl` to support the Wasm LinuxArjun Ramesh-1/+4
Interface This commit does not patch libc, stdarch, or cc
2025-03-07Rollup merge of #136667 - vita-rust:revert-vita-c-char, r=cuviperMatthias Krüger-2/+6
Revert vita's c_char back to i8 # Description Hi! https://github.com/rust-lang/rust/pull/132975 changed the definition of `c_char` from i8 to u8 for most ARM targets. While that would usually be correct, [VITASDK uses signed chars by default](https://github.com/vitasdk/buildscripts/blob/master/patches/gcc/0001-gcc-10.patch#L33-L34). The Clang definitions are incorrect because Clang is not (yet?) supported by the vita commmunity / `VITADSK`, On the Rust side, the pre-compiled libraries the user can link to are all compiled using vita's `gcc` and [we set `TARGET_CC` and `TARGET_CXX`](https://github.com/vita-rust/cargo-vita/blob/d564a132cbd43947118c0d6d0ebfbea7d1dd7fa7/src/commands/build.rs#L230) in `cargo vita` for build scripts using `cc`. I'm creating it as a draft PR so that we can discuss it and possibly get it approved here, but wait to merge the [libc side](https://github.com/rust-lang/libc/pull/4258) and get a libc version first, as having the definitions out of sync breaks std. As a nightly-only target it can be confusing/frustrating for new users when the latest nightly, which is the default, is broken.
2025-02-26Rollup merge of #136187 - hkBst:patch-27, r=workingjubileeLeón Orell Valerian Liehr-36/+37
Use less CString in the examples of CStr. Fixes #83999
2025-02-24remove uses of rustc_intrinsic_must_be_overridden from standard libraryRalf Jung-12/+3
2025-02-22Stabilise c_str_moduleltdk-1/+1
2025-02-21Do not use CString in the examples of CStr.Marijn Schouten-36/+37
Fixes #83999.
2025-02-20Implement 'PartialEq<{&Self, CString, Cow<Self>}>' for 'CStr'; Implement ↵Gabriel Bjørnager Jensen-0/+14
'PartialEq<{CStr, &CStr, Cow<CStr>}>' for 'CString'; Implement 'PartialEq<{CStr, &CStr, CString}>' for 'Cow<CStr>';
2025-02-09Mark extern blocks as unsafeMichael Goulet-2/+2
2025-02-06Revert vita's c_char back to i8Aphek-2/+6
2025-02-03primitive type migration from mod.rs to primitives.rsricci009-168/+181
2025-01-28Update comments and sort target_arch in c_char_definitionTaiki Endo-7/+9
2025-01-24ports last few library files to new intrinsic styleaaishwarymishra@gmail.com-12/+22
2025-01-20core: add `#![warn(unreachable_pub)]`Urgau-10/+10
2025-01-11Rename `pos` to `position`Yuri Astrakhan-6/+6
2025-01-11Convert `struct FromBytesWithNulError` into enumYuri Astrakhan-31/+19
One of `CStr` constructors, `CStr::from_bytes_with_nul(bytes: &[u8])` handles 3 cases: 1. `bytes` has one NULL as the last value - creates CStr 2. `bytes` has no NULL - error 3. `bytes` has a NULL in some other position - error The 3rd case is error that may require lossy conversion, but the 2nd case can easily be handled by the user code. Unfortunately, this function returns an opaque `FromBytesWithNulError` error in both 2nd and 3rd case, so the user cannot detect just the 2nd case - having to re-implement the entire function and bring in the `memchr` dependency. In [this code](https://github.com/gquintard/varnish-rs/blob/f86d7a87683b08d2e634d63e77d9dc1d24ed4a13/varnish-sys/src/vcl/ws.rs#L158), my FFI code needs to copy user's `&[u8]` into a C-allocated memory blob in a NUL-terminated `CStr` format. My code must first validate if `&[u8]` has a trailing NUL (case 1), no NUL (adds one on the fly - case 2), or NUL in the middle (3rd case - error). I had to re-implement `from_bytes_with_nul` and add `memchr`dependency just to handle the 2nd case. This PR renames the former `kind` enum from `FromBytesWithNulErrorKind` to `FromBytesWithNulError`, and removes the original struct.
2024-12-26docs: inline `core::ffi::c_str` types to `core::ffi`Michael Howell-2/+2
2024-12-10Add references to the specific ABI documentsAlex Richardson-7/+63
Expcept for L4RE and Xtensa these were obtained from #131319 I could not find an open link to the Xtensa documentation, but the signedness was confirmed by on of the Xtensa developers in https://github.com/llvm/llvm-project/pull/115967#issuecomment-2506292323 Co-authored-by: Taiki Endo <te316e89@gmail.com>
2024-12-10Remove l4re from the unsigned char operating system listAlex Richardson-2/+2
As noted in https://github.com/rust-lang/rust/pull/132975#issuecomment-2484645240, the default for userland apps is to follow the architecture defaults, the -funsigned-char flag only applies to kernel builds.
2024-12-10De-duplicate and improve definition of core::ffi::c_charAlex Richardson-53/+24
Instead of having a list of unsigned char targets for each OS, follow the logic Clang uses and instead set the value based on architecture with a special case for Darwin and Windows operating systems. This makes it easier to support new operating systems targeting Arm/AArch64 without having to modify this config statement for each new OS. The new list does not quite match Clang since I noticed a few bugs in the Clang implementation (https://github.com/llvm/llvm-project/issues/115957). Fixes: https://github.com/rust-lang/rust/issues/129945
2024-12-03Teach rust core about Xtensa VaListImpl and add a custom lowering of vaarg ↵Brian J. Tarricone-0/+19
for xtensa. LLVM does not include an implementation of the va_arg instruction for Xtensa. From what I understand, this is a conscious decision and instead language frontends are encouraged to implement it themselves. The rationale seems to be that loading values correctly requires language and ABI-specific knowledge that LLVM lacks. This is true of most architectures, and rustc already provides implementation for a number of them. This commit extends the support to include Xtensa. See https://lists.llvm.org/pipermail/llvm-dev/2017-August/116337.html for some discussion on the topic. Unfortunately there does not seem to be a reference document for the semantics of the va_list and va_arg on Xtensa. The most reliable source is the GCC implementation, which this commit tries to follow. Clang also provides its own compatible implementation. This was tested for all the types that rustc allows in variadics. Co-authored-by: Brian Tarricone <brian@tarricone.org> Co-authored-by: Jonathan Bastien-Filiatrault <joe@x2a.org> Co-authored-by: Paul Lietar <paul@lietar.net>
2024-11-27update cfgsBoxy-6/+2