save cloning by passing by ref to persistence functions

pull/9143/head
Ingrid 9 months ago
parent ca16809d75
commit 588e0b4b93

@ -6,7 +6,7 @@ use std::{
path::PathBuf,
};
pub fn push_history<T: Serialize>(filepath: PathBuf, entry: T) {
pub fn push_history<T: Serialize>(filepath: PathBuf, entry: &T) {
let file = OpenOptions::new()
.append(true)
.create(true)
@ -15,7 +15,7 @@ pub fn push_history<T: Serialize>(filepath: PathBuf, entry: T) {
.unwrap();
// TODO: do something about this unwrap
serialize_into(file, &entry).unwrap();
serialize_into(file, entry).unwrap();
}
pub fn read_history<T: for<'a> Deserialize<'a>>(filepath: PathBuf) -> Vec<T> {

@ -1765,7 +1765,11 @@ impl Editor {
// We need to persist ViewPositions on documents a la PR #7568, then fetch that here.
if doc.selections().contains_key(&id) {
if let Some(path) = doc.path() {
file_locs.push((path.clone(), offset, doc.selection(id).clone()));
file_locs.push(FileHistoryEntry::new(
path.clone(),
offset,
doc.selection(id).clone(),
));
}
}
@ -1774,13 +1778,9 @@ impl Editor {
}
for loc in file_locs {
// TODO: can the arg here be a reference? would save cloning
persistence::push_file_history(FileHistoryEntry::new(
loc.0.clone(),
loc.1,
loc.2.clone(),
));
self.old_file_locs.insert(loc.0, (loc.1, loc.2));
persistence::push_file_history(&loc);
self.old_file_locs
.insert(loc.path, (loc.view_position, loc.selection));
}
self.tree.remove(id);
@ -1820,7 +1820,11 @@ impl Editor {
if view.doc == doc_id {
if let Some(path) = doc.path() {
file_locs.push((path.clone(), view.offset, doc.selection(view.id).clone()));
file_locs.push(FileHistoryEntry::new(
path.clone(),
view.offset,
doc.selection(view.id).clone(),
));
};
// something was previously open in the view, switch to previous doc
@ -1837,13 +1841,9 @@ impl Editor {
.collect();
for loc in file_locs {
// TODO: can the arg here be a reference? would save cloning
persistence::push_file_history(FileHistoryEntry::new(
loc.0.clone(),
loc.1,
loc.2.clone(),
));
self.old_file_locs.insert(loc.0, (loc.1, loc.2));
persistence::push_file_history(&loc);
self.old_file_locs
.insert(loc.path, (loc.view_position, loc.selection));
}
for action in actions {

@ -26,7 +26,7 @@ impl FileHistoryEntry {
}
}
pub fn push_file_history(entry: FileHistoryEntry) {
pub fn push_file_history(entry: &FileHistoryEntry) {
push_history(file_histfile(), entry)
}
@ -34,7 +34,7 @@ pub fn read_file_history() -> Vec<FileHistoryEntry> {
read_history(file_histfile())
}
pub fn push_reg_history(register: char, line: &str) {
pub fn push_reg_history(register: char, line: &String) {
let filepath = match register {
':' => command_histfile(),
'/' => search_histfile(),

Loading…
Cancel
Save