MCPcopy Create free account
hub / github.com/bytecodealliance/system-interface / read_to_string_at

Function read_to_string_at

src/fs/file_io_ext.rs:1058–1076  ·  view source on GitHub ↗
(file: &std::fs::File, buf: &mut String, offset: u64)

Source from the content-addressed store, hash-verified

1056}
1057
1058fn read_to_string_at(file: &std::fs::File, buf: &mut String, offset: u64) -> io::Result<usize> {
1059 let len = match file.metadata()?.len().checked_sub(offset) {
1060 None => return Ok(0),
1061 Some(len) => len,
1062 };
1063
1064 // This temporary buffer is theoretically unnecessary, but eliminating it
1065 // currently involves a bunch of `unsafe`.
1066 let mut tmp = vec![0_u8; len.try_into().unwrap_or(usize::MAX)];
1067 FileIoExt::read_exact_at(file, &mut tmp, offset)?;
1068 let s = String::from_utf8(tmp).map_err(|_| {
1069 io::Error::new(
1070 io::ErrorKind::InvalidData,
1071 "stream did not contain valid UTF-8",
1072 )
1073 })?;
1074 buf.push_str(&s);
1075 Ok(len as usize)
1076}
1077
1078fn _file_io_ext_can_be_trait_object(_: &dyn FileIoExt) {}

Callers 1

read_to_string_atMethod · 0.70

Calls 1

read_exact_atFunction · 0.85

Tested by

no test coverage detected