refactor: prefer Arc<str> to String

This commit is contained in:
2026-05-23 18:45:44 +01:00
parent 1ef91cb41e
commit 1843622540
15 changed files with 524 additions and 544 deletions

View File

@@ -20,10 +20,10 @@ pub(crate) struct LineDiff {
pub(crate) fn classify_content(content: &[u8]) -> ContentType {
if content.is_empty() {
ContentType::Text
} else if content.starts_with(&[0xEF, 0xBB, 0xBF]) // UTF-8
} else if content.starts_with(&[0xEF, 0xBB, 0xBF]) // UTF-8
|| content.starts_with(&[0x00, 0x00, 0xFE, 0xFF]) // UTF-32 BE
|| content.starts_with(&[0xFF, 0xFE, 0x00, 0x00]) // UTF-32 LE
|| content.starts_with(&[0xFE, 0xFF]) // UTF-16 BE
|| content.starts_with(&[0xFE, 0xFF]) // UTF-16 BE
|| content.starts_with(&[0xFF, 0xFE])
{
ContentType::Text
@@ -34,15 +34,3 @@ pub(crate) fn classify_content(content: &[u8]) -> ContentType {
}
}
}
pub(crate) fn diff_content(old: &[u8], new: &[u8]) -> ContentDiff {
similar::TextDiff::from_lines::<[u8]>(old, new)
.iter_all_changes()
.map(|change| LineDiff {
old_line: change.old_index(),
old_content_range: change.old_range,
new_line: change.new_index(),
new_content_range: change.new_range,
})
.collect()
}

View File

@@ -1,3 +1,4 @@
pub(crate) mod diff;
pub(crate) mod file;
pub(crate) mod str;
pub(crate) mod timeout;

20
src/util/str.rs Normal file
View File

@@ -0,0 +1,20 @@
use std::sync::Arc;
use crate::api;
pub(crate) trait ToSharedString {
fn to_shared_string(&self) -> gpui::SharedString;
}
impl ToSharedString for Arc<str> {
/// converts into gpui SharedString cheaply with no allocation involved.
fn to_shared_string(&self) -> gpui::SharedString {
gpui::SharedString::new(Arc::clone(self))
}
}
impl Into<gpui::ElementId> for api::issues::Id {
fn into(self) -> gpui::ElementId {
gpui::ElementId::Name(gpui::SharedString::new(Arc::clone(&self.0)))
}
}