`std::borrow::Cow<'_, str>` now implements `TextBuffer` (#3164)
* `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 <emil.ernerfeldt@gmail.com>
This commit is contained in:
parent
ad8b41cad6
commit
08c46acba1
|
|
@ -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 {
|
||||
<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 {
|
||||
|
|
|
|||
Loading…
Reference in New Issue