Skip to content

Commit

Permalink
std::borrow::Cow<'_, str> now implements TextBuffer (#3164)
Browse files Browse the repository at this point in the history
* `std::borrow::Cow<'_, str>` now implements `TextBuffer`

* Add pr number

* Remove line from CHANGELOG.md

* Add missing semicolons

* remove extra semicolon

---------

Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
burtonageo and emilk authored Sep 18, 2023
1 parent ad8b41c commit 08c46ac
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion crates/egui/src/widgets/text_edit/text_buffer.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 {
<String as TextBuffer>::insert_text(self.to_mut(), text, char_index)
}

fn delete_char_range(&mut self, char_range: Range<usize>) {
<String as TextBuffer>::delete_char_range(self.to_mut(), char_range);
}

fn clear(&mut self) {
<String as TextBuffer>::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 {
Expand Down

0 comments on commit 08c46ac

Please sign in to comment.