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

Relax lifetime restrictions on as_raw_buffer, as_raw_nopadding_buffer, and save_as_image for windows_capture::frame::FrameBuffer #41

Merged

Conversation

winestone
Copy link
Contributor

@winestone winestone commented Apr 28, 2024

We probably intend these lifetimes to look like:

 impl<'a> FrameBuffer<'a> {
-    pub fn as_raw_buffer(&'a mut self) -> &'a mut [u8] {
+    pub fn as_raw_buffer<'b>(&'b mut self) -> &'b mut [u8] {
         self.raw_buffer
     }

which is equivalent to letting the lifetimes be inferred I think.

Hope this PR helps, let me know if there are any changes you'd like, thanks!


For some context, this was preventing me from writing a function like:

    fn from_frame_buffer<'a, 'fb: 'a>(frame_buffer: &'a mut FrameBuffer<'fb>) -> Self {
        let buffer = frame_buffer.as_raw_buffer().to_vec();
        let width = frame_buffer.width();

and I would get an error like:

error[E0502]: cannot borrow `*frame_buffer` as immutable because it is also borrowed as mutable
  --> src/main.rs:45:26
   |
43 |     fn from_frame_buffer<'a, 'fb: 'a>(frame_buffer: &'a mut FrameBuffer<'fb>) -> Self {
   |                              --- lifetime `'fb` defined here
44 |         let buffer: Vec<u8> = frame_buffer.as_raw_buffer().to_vec();
   |                               ----------------------------
   |                               |
   |                               mutable borrow occurs here
   |                               argument requires that `*frame_buffer` is borrowed for `'fb`
45 |         let width: u32 = frame_buffer.width();
   |                          ^^^^^^^^^^^^ immutable borrow occurs here

Essentially when I call frame_buffer.as_raw_buffer(), it tries to borrow it for the lifetime of 'fb above, which is probably not what we want (I just wanna borrow it just for this statement, not for the whole 'fb lifetime).

@winestone winestone changed the title Relax lifetime restrictions on as_raw_buffer, as_raw_nopadding_buffer, and save_as_image<T for windows_capture::frame::FrameBuffer Relax lifetime restrictions on as_raw_buffer, as_raw_nopadding_buffer, and save_as_image for windows_capture::frame::FrameBuffer Apr 28, 2024
…fer`, and `save_as_image` for `windows_capture::frame::FrameBuffer`
@winestone winestone force-pushed the relax-framebuffer-lifetimes branch from 1f43f6f to 47d4a5a Compare April 28, 2024 14:53
@NiiightmareXD
Copy link
Owner

Nice, thanks.

@NiiightmareXD NiiightmareXD merged commit 6536cd3 into NiiightmareXD:main Apr 28, 2024
2 checks passed
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.

2 participants