Conversation
Types wrapping `&FontSystem` instead need to have methods taking an `&mut FontSystem`. Eliminating the lifetime bounds make the API more usable, but perhaps too many methods require this to be passed. Moving interior mutability to the caller means it isn't used unless needed, and the caller can use a `Mutex`, some nostd alternative, or a `RefCell`. This updates the `editor-orbclient` to compile with the API changes. The others need to be updated, but perhaps not until any changes to make methods not require an `&mut FontSystem` if possible.
hecrj
left a comment
There was a problem hiding this comment.
Getting rid of the Buffer lifetime is great. It'll help me get rid of ouroboros in my iced integration.
Perhaps as future work, to recover some of the ergonomics, when you are calling methods on the same instance repeatedly, so that you're not always passing fn main() {
let mut font_system = FontSystem::new();
let param = ();
let mut buffer = Buffer::new();
{
let mut buffer = buffer.with_font_system(&mut font_system);
buffer.method(param);
}
}
/// Gets dropped at the end of the scope, releasing the borrows
struct WithFontSystem<'a, T> {
inner: &'a mut T,
font_system: &'a mut FontSystem,
}
impl Buffer {
fn method(&mut self, font_system: &mut FontSystem, param: Param) {
todo!()
}
fn with_font_system<'a>(&'a mut self, font_system: &'a mut FontSystem) -> WithFontSystem<'a, Self> {
WithFontSystem {
inner: self,
font_system,
}
}
}
impl<'a> WithFontSystem<'a, Buffer> {
// ... reimplement all of the methods on `Buffer`, simply forwarding to the relevant `Buffer` methods
fn method(&mut self, param: Param) {
self.inner.method(&mut self.font_system, param);
}
} |
|
@ids1024 @tigregalis Thank you for your ideas, which I have incorporated in #97. With #97 being merged, I think this can be closed? |
Types wrapping
&FontSysteminstead need to have methods taking an&mut FontSystem. Eliminating the lifetime bounds make the API more usable, but perhaps too many methods require this to be passed.Moving interior mutability to the caller means it isn't used unless needed, and the caller can use a
Mutex, some nostd alternative, or aRefCell.This updates the
editor-orbclientto compile with the API changes. The others need to be updated, but perhaps not until any changes to make methods not require an&mut FontSystemif possible.