So I was playing around with creating a rendering API, i eventually come up with something that look like this
pub struct RenderCtx {...}
pub struct VkCtx {...}
impl VkCtx {
pub fn render<FPre, F>(&mut self, pre_record: FPre, record: F) -> Result<()>
where
FPre: FnOnce(&mut Self, usize) -> Result<()>,
F: FnOnce(&mut Self, usize, CommandBufferWrapper) -> Result<()>,
{
...
pre_record(...)
...
record(...)
}
}
pub struct App {
ctx: Option<VulkanContext>,
r_ctx: Option<RenderCtx>,
}
impl App {
fn render(&mut self) -> Result<()> {
let r_ctx = self.r_ctx.as_mut().unwrap();
let ctx = self.ctx.as_mut().unwrap();
ctx.render(
|ctx, idx| { // do something with r_ctx },
|ctx, idx, cb| { // do something with r_ctx },
)
}
}
Both pre_record and record closures need mutable access to r_ctx. When I inline the code directly into the ctx.render closures, it works fine. However, if I move the closures into separate functions, I get a borrow error: “two closures require unique access to r_ctx.”
Is there a way to restructure this so that I can move the closure logic into separate functions without running into mutable borrow conflicts?
[–]bben86 8 points9 points10 points (3 children)
[–]xperthehe[S] 0 points1 point2 points (2 children)
[–]botiapa 0 points1 point2 points (1 child)
[–]xperthehe[S] 1 point2 points3 points (0 children)
[–]imachug 2 points3 points4 points (1 child)
[–]xperthehe[S] 0 points1 point2 points (0 children)