pub struct WeakEditorHandle { /* private fields */ }Expand description
An EditorHandle that does not keep its editor alive.
For a callback the editor itself stores. on_image_activated,
on_image_resized, on_link_activated, on_files_dropped, on_change,
on_text_inserted and the image resolver are all kept on the editor’s own
state, so a handler that captures an EditorHandle by value makes the state
own itself. Nothing can break that ring afterwards: the widget can be
destroyed, its tree dropped and its window closed, and the editor — with its
document, its cursor and its shaped layout — stays resident for the life of
the process. It is a leak with no owner left to blame, and it is easy to write,
because reaching for editor.handle() is the obvious way for such a handler to
act on the editor it belongs to.
Capture this instead and upgrade inside the handler. The
handler runs only while the editor is alive, which is the only time it could
have done anything anyway.
let editor = RichTextEditor::editor(doc);
let weak = editor.handle().downgrade();
let editor = editor.on_image_activated(move |activation, _ctx| {
let Some(handle) = weak.upgrade() else { return };
handle.select_range(activation.offset, activation.offset + 1);
});A factory the builder stores rather than the state —
context_menu is the one today — may hold a
strong handle safely, because it dies with the widget.
Implementations§
Source§impl WeakEditorHandle
impl WeakEditorHandle
Sourcepub fn upgrade(&self) -> Option<EditorHandle>
pub fn upgrade(&self) -> Option<EditorHandle>
The handle, if its editor is still alive.
Trait Implementations§
Source§impl Clone for WeakEditorHandle
impl Clone for WeakEditorHandle
Source§fn clone(&self) -> WeakEditorHandle
fn clone(&self) -> WeakEditorHandle
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for WeakEditorHandle
impl !Send for WeakEditorHandle
impl !Sync for WeakEditorHandle
impl !UnwindSafe for WeakEditorHandle
impl Freeze for WeakEditorHandle
impl Unpin for WeakEditorHandle
impl UnsafeUnpin for WeakEditorHandle
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more