Borrowck problems with closures


#1

I am trying to reduce some highly repetitive code using a closure as a
call-back function. However, I am running into borrowck issues, since
the existance of the closure in scope prevents me from calling a
function on self.

The code looks like this:

fn object_binary(&mut self, o:&ObjectProperty, ce: &ClassExpression,
                 tag:&[u8]) {
    let f = || {
        self.object_property(o);
        self.class_expression(ce);
    };

    self.write_start_end(tag, f);
}


fn write_start_end<F>(&mut self, tag:&[u8], mut contents:F )
    where F: FnMut()
{
    let len = tag.len();
    let open = BytesStart::borrowed(tag,len);
    self.writer.write_event(Event::Start(open)).ok();

    contents();

    self.writer
        .write_event(Event::End(BytesEnd::borrowed(tag)))
        .ok();
}

Is there any way around this?


#2

Using argument first that comes to mind.

let f = |s| {
        s.object_property(o);
        s.class_expression(ce);
    };
where F: FnMut(&mut Self)
contents(self);

#3

@jonh Yes we are right that works. Slightly clunky but effective.