`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:
George Burton 2023-09-18 20:38:10 +01:00 committed by GitHub
parent ad8b41cad6
commit 08c46acba1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 1 deletions

View File

@ -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 {