Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return slices with 'data lifetime in BorrowedBuf #132532

Closed
wants to merge 1 commit into from

Conversation

SUPERCILEX
Copy link
Contributor

Tracking issue: #117693

Seems like an oversight? I don't think there's any reason to limit the lifetime of the filled buffer... fixing this enables the following style of use case:

pub fn direct_file_name(
    buf: &mut [MaybeUninit<u8>; DIRECT_FILE_NAME_LEN + 1],
    to: RingKind,
    index: u32,
) -> DirectFileNameToken<()> {
    let mut buf = BorrowedBuf::from(buf.as_mut_slice());
    write!(buf.unfilled(), "{:0>13}\0", composite_id(to, index)).unwrap();
    DirectFileNameToken(buf.filled_mut(), PhantomData) // Doesn't compile!
}

@rustbot
Copy link
Collaborator

rustbot commented Nov 2, 2024

r? @Amanieu

rustbot has assigned @Amanieu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 2, 2024
@SUPERCILEX SUPERCILEX changed the title Return slices with 'data lifetime BorrowedBuf Return slices with 'data lifetime in BorrowedBuf Nov 2, 2024
@rust-log-analyzer
Copy link
Collaborator

The job mingw-check failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#16 3.093 Building wheels for collected packages: reuse
#16 3.094   Building wheel for reuse (pyproject.toml): started
#16 3.341   Building wheel for reuse (pyproject.toml): finished with status 'done'
#16 3.342   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132720 sha256=026f3bb0f1aa8090b861fd0a0939cb1a782396d84c8aab7875096557d637a0f6
#16 3.342   Stored in directory: /tmp/pip-ephem-wheel-cache-yp186m7o/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#16 3.345 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#16 3.730 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#16 3.731 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#16 DONE 3.8s
---
   Compiling std v0.0.0 (/checkout/library/std)
error: lifetime may not live long enough
  --> core/src/io/borrowed_buf.rs:97:13
   |
72 | impl<'data> BorrowedBuf<'data> {
   |      ----- lifetime `'data` defined here
...
93 |     pub fn filled(&self) -> &'data [u8] {
   |                   - let's call the lifetime of this reference `'1`
97 |             MaybeUninit::slice_assume_init_ref(buf)
97 |             MaybeUninit::slice_assume_init_ref(buf)
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ method was supposed to return data with lifetime `'data` but it is returning data with lifetime `'1`
error: lifetime may not live long enough
   --> core/src/io/borrowed_buf.rs:105:9
    |
    |
72  |   impl<'data> BorrowedBuf<'data> {
    |        ----- lifetime `'data` defined here
103 |       pub fn filled_mut(&mut self) -> &'data mut [u8] {
    |                         - let's call the lifetime of this reference `'1`
    |                         - let's call the lifetime of this reference `'1`
104 |           // SAFETY: We only slice the filled part of the buffer, which is always valid
105 | /         unsafe {
106 | |             let buf = self.buf.get_unchecked_mut(..self.filled);
107 | |             MaybeUninit::slice_assume_init_mut(buf)
108 | |         }
    | |_________^ method was supposed to return data with lifetime `'data` but it is returning data with lifetime `'1`
error: could not compile `core` (lib) due to 2 previous errors
Build completed unsuccessfully in 0:00:43
  local time: Sat Nov  2 17:55:57 UTC 2024
  network time: Sat, 02 Nov 2024 17:55:57 GMT

@SUPERCILEX
Copy link
Contributor Author

Ah right this doesn't work because you could alias. Will make an ACP.

@SUPERCILEX SUPERCILEX closed this Nov 2, 2024
@SUPERCILEX
Copy link
Contributor Author

ACP: rust-lang/libs-team#473

@SUPERCILEX SUPERCILEX deleted the patch-3 branch December 27, 2024 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants