diff --git a/crates/egui/src/widgets/text_edit/text_buffer.rs b/crates/egui/src/widgets/text_edit/text_buffer.rs index e29a07a9ff5..a97d264a15c 100644 --- a/crates/egui/src/widgets/text_edit/text_buffer.rs +++ b/crates/egui/src/widgets/text_edit/text_buffer.rs @@ -1,4 +1,4 @@ -use std::ops::Range; +use std::{borrow::Cow, ops::Range}; /// Trait constraining what types [`crate::TextEdit`] may use as /// an underlying buffer. @@ -100,6 +100,36 @@ impl TextBuffer for String { } } +impl<'a> TextBuffer for Cow<'a, str> { + fn is_mutable(&self) -> bool { + true + } + + fn as_str(&self) -> &str { + self.as_ref() + } + + fn insert_text(&mut self, text: &str, char_index: usize) -> usize { + ::insert_text(self.to_mut(), text, char_index) + } + + fn delete_char_range(&mut self, char_range: Range) { + ::delete_char_range(self.to_mut(), char_range); + } + + fn clear(&mut self) { + ::clear(self.to_mut()); + } + + fn replace(&mut self, text: &str) { + *self = Cow::Owned(text.to_owned()); + } + + fn take(&mut self) -> String { + std::mem::take(self).into_owned() + } +} + /// Immutable view of a `&str`! impl<'a> TextBuffer for &'a str { fn is_mutable(&self) -> bool {