Skip to content

Conversation

@gmorer
Copy link

@gmorer gmorer commented Dec 1, 2025

The lifetime returned by RawDir::next() is the RawDir's lifetime and not the buffer it uses ('buf). The file name lives inside the buffer provided to the kernel.

Example

fn test<'buf, 'fd>(fd: BorrowedFd<'fd>, buf: &'buf mut Vec<u8>) -> RawDirEntry<'buf> {
    RawDir::new(fd, buf.spare_capacity_mut())
        .next()
        .unwrap()
        .unwrap()
}

Without the change, the borrow checker will tell the following error:

error: lifetime may not live long enough
  --> brr-lib/src/fs/dir.rs:40:5
   |
   |   fn test<'buf, 'fd>(fd: BorrowedFd<'fd>, buf: &'buf mut Vec<u8>) -> RawDirEntry<'buf> {
   |           -----  --- lifetime `'fd` defined here
   |           |
   |           lifetime `'buf` defined here
   | /     RawDir::new(fd, buf.spare_capacity_mut())
   | |         .next()
   | |         .unwrap()
   | |         .unwrap()
   | |_________________^ function was supposed to return data with lifetime `'buf` but it is returning data with lifetime `'fd`
   |
   = help: consider adding the following bound: `'fd: 'buf`

error[E0515]: cannot return value referencing temporary value
  --> brr-lib/src/fs/dir.rs:40:5
   |
   |       RawDir::new(fd, buf.spare_capacity_mut())
   |       ^----------------------------------------
   |       |
   |  _____temporary value created here
   | |
   | |         .next()
   | |         .unwrap()
   | |         .unwrap()
   | |_________________^ returns a value referencing data owned by the current function

The borrow checker passes with the change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant