rust icon indicating copy to clipboard operation
rust copied to clipboard

Add CStr::bytes iterator

Open clarfonthey opened this issue 1 year ago • 14 comments

See rust-lang/libs-team#135 for an ACP.

Since rust-lang/libs-team#134 was also accepted, this type is now core::ffi::c_str::Bytes instead of core::ffi::CStrBytes.

clarfonthey avatar Nov 13 '22 09:11 clarfonthey

r? @thomcc

(rustbot has picked a reviewer for you, use r? to override)

rustbot avatar Nov 13 '22 09:11 rustbot

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

rustbot avatar Nov 13 '22 09:11 rustbot

The job x86_64-gnu-llvm-13 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
   Compiling unwind v0.0.0 (/checkout/library/unwind)
error[E0412]: cannot find type `Bytes` in this scope
   --> library/core/src/ffi/c_str.rs:608:28
    |
608 |     pub fn bytes(&self) -> Bytes<'_> {
    |
help: consider importing this struct
    |
1   | use crate::str::Bytes;
---

error[E0308]: mismatched types
   --> library/core/src/ffi/c_str.rs:710:50
    |
710 |             ptr: unsafe { NonNull::new_unchecked(s.inner.as_ptr()) },
    |                           ---------------------- ^^^^^^^^^^^^^^^^ types differ in mutability
    |                           arguments to this function are incorrect
    |
    |
    = note: expected raw pointer `*mut u8`
               found raw pointer `*const i8`
   --> library/core/src/ptr/non_null.rs:197:25
    |
    |
197 |     pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {


error[E0560]: struct `CStrBytes<'a>` has no field named `phantom`
    |
    |
711 |             phantom: PhantomData,
    |             ^^^^^^^ `CStrBytes<'a>` does not have this field
    |
    = note: available fields are: `ptr`, `marker`

error[E0614]: type `NonNull<u8>` cannot be dereferenced
    |
    |
720 |         unsafe { *self.ptr == 0 }

error[E0308]: mismatched types
   --> library/core/src/ffi/c_str.rs:729:33
    |
    |
729 |         unsafe { CStr::from_ptr(self.ptr.as_ptr()) }
    |                  -------------- ^^^^^^^^^^^^^^^^^ expected `i8`, found `u8`
    |                  |
    |                  arguments to this function are incorrect
    |
    = note: expected raw pointer `*const i8`
               found raw pointer `*mut u8`
   --> library/core/src/ffi/c_str.rs:259:25
    |
    |
259 |     pub const unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {


error[E0614]: type `NonNull<u8>` cannot be dereferenced
    |
756 |             let ret = *self.ptr;
    |                       ^^^^^^^^^

rust-log-analyzer avatar Nov 13 '22 09:11 rust-log-analyzer

This seems fairly reasonable to me. Can you file an ACP though?

thomcc avatar Nov 13 '22 19:11 thomcc

Sounds reasonable to me! Filed those and replaced the description with a link back to the ACP.

clarfonthey avatar Nov 15 '22 14:11 clarfonthey

Since the ACP was accepted, I removed the as_c_str and AsRef impl and opened a tracking issue. Adding the removed bits was mentioned as an unanswered question in the tracking issue since it was not accepted as part of the ACP.

clarfonthey avatar May 30 '23 19:05 clarfonthey

@rustbot ready

(not sure if I can just do this, or if someone else will have to)

(edit: looks like I can, but it doesn't remove the S-waiting-on-ACP tag. someone else will have to do that)

clarfonthey avatar May 30 '23 20:05 clarfonthey

@rustbot blocked

Going to open a PR in light of this ACP being accepted: rust-lang/libs-team#134

Then just make this depend on that one being merged first.

clarfonthey avatar May 31 '23 11:05 clarfonthey

:umbrella: The latest upstream changes (presumably #114443) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Sep 20 '23 02:09 bors

I'm going to be away for a few months, so I'm rerolling my PRs so that folks don't have to wait for me. Sorry/thanks.

r? libs

thomcc avatar Feb 01 '24 22:02 thomcc

r? libs

joshtriplett avatar Feb 11 '24 02:02 joshtriplett

@rustbot author

(want to verify tests pass before I mark as ready; blocker has been merged)

clarfonthey avatar Mar 10 '24 20:03 clarfonthey

@rustbot ready

clarfonthey avatar Mar 11 '24 01:03 clarfonthey

Oh, I'm terrible with batching review comments, so, I get that. Will poke around the code in a bit to see what I can do.

clarfonthey avatar Mar 11 '24 22:03 clarfonthey

@bors r+ rollup

cuviper avatar Mar 13 '24 04:03 cuviper

:pushpin: Commit a38a556ceb4b65c397a47bcbe35d004bc9b8626f has been approved by cuviper

It is now in the queue for this repository.

bors avatar Mar 13 '24 04:03 bors