cleanup: avoid some unnecessary uses of Cursor

Most of our uses of `Cursor` can be replaced by just mutable slices,
which seems simpler, so I guess that's preferred.
This commit is contained in:
Martin von Zweigbergk 2023-05-30 17:17:06 -07:00 committed by Martin von Zweigbergk
parent a5ef4f0f40
commit 3ca42908ee
3 changed files with 8 additions and 10 deletions

View file

@ -13,7 +13,7 @@
// limitations under the License. // limitations under the License.
use std::cmp::max; use std::cmp::max;
use std::io::{Cursor, Write}; use std::io::Write;
use itertools::Itertools; use itertools::Itertools;
@ -264,7 +264,7 @@ pub fn conflict_to_materialized_value(
) -> TreeValue { ) -> TreeValue {
let mut buf = vec![]; let mut buf = vec![];
materialize_conflict(store, path, conflict, &mut buf).unwrap(); materialize_conflict(store, path, conflict, &mut buf).unwrap();
let file_id = store.write_file(path, &mut Cursor::new(&buf)).unwrap(); let file_id = store.write_file(path, &mut buf.as_slice()).unwrap();
TreeValue::File { TreeValue::File {
id: file_id, id: file_id,
executable: false, executable: false,
@ -426,7 +426,7 @@ pub fn update_conflict_from_content(
// with conflict markers. Update the Conflict object with the new // with conflict markers. Update the Conflict object with the new
// FileIds. // FileIds.
for (i, buf) in removed_content.iter().enumerate() { for (i, buf) in removed_content.iter().enumerate() {
let file_id = store.write_file(path, &mut Cursor::new(buf))?; let file_id = store.write_file(path, &mut buf.as_slice())?;
if let TreeValue::File { id, executable: _ } = &mut conflict.removes[i].value { if let TreeValue::File { id, executable: _ } = &mut conflict.removes[i].value {
*id = file_id; *id = file_id;
} else { } else {
@ -437,7 +437,7 @@ pub fn update_conflict_from_content(
} }
} }
for (i, buf) in added_content.iter().enumerate() { for (i, buf) in added_content.iter().enumerate() {
let file_id = store.write_file(path, &mut Cursor::new(buf))?; let file_id = store.write_file(path, &mut buf.as_slice())?;
if let TreeValue::File { id, executable: _ } = &mut conflict.adds[i].value { if let TreeValue::File { id, executable: _ } = &mut conflict.adds[i].value {
*id = file_id; *id = file_id;
} else { } else {

View file

@ -18,7 +18,7 @@ use std::collections::{BTreeMap, BTreeSet, BinaryHeap, Bound, HashMap, HashSet};
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::fs::File; use std::fs::File;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::io::{Cursor, Read, Write}; use std::io::{Read, Write};
use std::ops::Range; use std::ops::Range;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::Arc; use std::sync::Arc;
@ -670,9 +670,8 @@ impl MutableIndexImpl {
file.write_all(&buf)?; file.write_all(&buf)?;
persist_content_addressed_temp_file(temp_file, index_file_path)?; persist_content_addressed_temp_file(temp_file, index_file_path)?;
let mut cursor = Cursor::new(&buf);
ReadonlyIndexImpl::load_from( ReadonlyIndexImpl::load_from(
&mut cursor, &mut buf.as_slice(),
dir, dir,
index_file_id_hex, index_file_id_hex,
commit_id_length, commit_id_length,

View file

@ -22,7 +22,7 @@ use std::cmp::Ordering;
use std::collections::{BTreeMap, HashMap}; use std::collections::{BTreeMap, HashMap};
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::{Cursor, Read, Write}; use std::io::{Read, Write};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, RwLock}; use std::sync::{Arc, RwLock};
@ -337,8 +337,7 @@ impl MutableTable {
file.write_all(&buf)?; file.write_all(&buf)?;
persist_content_addressed_temp_file(temp_file, file_path)?; persist_content_addressed_temp_file(temp_file, file_path)?;
let mut cursor = Cursor::new(&buf); ReadonlyTable::load_from(&mut buf.as_slice(), store, file_id_hex, store.key_size)
ReadonlyTable::load_from(&mut cursor, store, file_id_hex, store.key_size)
} }
} }