“Blitzen, do you realize every single read_to_string()
call triggers a fresh heap allocation? Do you think we’ve got infinite memory lying around?”
Here, I called it a few times and the process is taking up 1.5GB
of memory, you think this is acceptable?
Blitzen glanced up, nonchalant. “Heap allocations happen, Santa. That’s life.”
Santa’s eyes narrowed. “Do you want the nightmare of Day 1 all over again? That disaster was just a simple string clone, and it nearly broke us. This? This is worse. Way worse. I don’t want to end up manually running drop()
just to keep things from spiraling into chaos!”
Blitzen stiffened, finally taking the hint. “Okay, okay. What do you want me to do?”
Prancer cut in cautiously. “Why not cache the file content on every write? And return a reference to the cached content on every read?”
Blitzen nodded slowly. “Yeah, that’ll work. No more redundant allocations, no stale reads, and we don’t touch drop()
at all.”
Santa’s glare softened—marginally. “Good. Make it lean, make it fast, and make sure it’s ready by sunrise. We don't have much time, Christmas is just around the corner.”
Santa's files are huge, and they use a lot of memory, To prevent that we need to create a new method named read_to_str
that will return a string slice instead of an owned value.
Here is what you need to do:
content
write
method to update the content
on every writeread_from_cache
method to return a string slice instead of an owned valueIf you’re unsure where to start, take a look at these tips:
Create a new field called content
to store the file content
pub struct TempFile {
file: File,
file_path: PathBuf,
content: Vec<u8>,
}
Update the field in file writes, use to_vec()
to turn a &[u8]
to a Vec<u8>
self.content = data.to_vec();
Use the std::str::from_utf8
function to convert a &[u8]
to a &str
std::str::from_utf8(&self.content).unwrap_or("")
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new(), }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("default") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new(), }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("default") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content: Vec<u8> = vec![]; Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content.clear(); self.content.extend(data); self.file.write_all(data)?; Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(self.content.as_slice()).unwrap() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: String}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = String::new(); Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = String::from_utf8(data.to_vec()).map_err(|_| { std::io::Error::new(std::io::ErrorKind::InvalidData, "Can't convert u8 slice to string") })?; Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice self.content.as_str() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new()}) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") // 3. Update this method to return the content as a string slice } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: vec![], }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content.write_all(data)?; self.content.clear(); self.content.extend_from_slice(data); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read(&mut self.content[..])?; Ok(self.read_from_cache().to_string()) } pub fn read_to_str(&self) -> &str { self.read_from_cache() } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: vec![] }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("Can't read from cache buffer.") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; //self.content.extend(data.to_vec()); self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice //self.content.iter().collect() std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};use std::str;pub struct TempFile<'a> { file_path: PathBuf, file: File, // Alternative with borrowed byte string content: &'a [u8],}impl<'a> TempFile<'a> { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content: &[u8; 0] = &[]; Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &'a[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data; Ok(()) } pub fn read_from_cache(&self) -> &str { str::from_utf8(self.content).unwrap() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl<'a> Drop for TempFile<'a> { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: String,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = String::new(); Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = String::from_utf8_lossy(data).into_owned(); Ok(()) } pub fn read_from_cache(&self) -> &str { &self.content[..] } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new(), }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content = data.to_vec(); self.file.write_all(data)?; Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content.clear(); self.content.extend_from_slice(data); self.file.write_all(data)?; Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(self.content.as_slice()).expect("valid UTF-8") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: String,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: String::new(), }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write(data)?; self.content = String::from_utf8(data.to_vec()).unwrap(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice &self.content[..] } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::<u8>::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file , content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = Vec::new(); Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: vec![] }) } pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::str::from_utf8;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; // This is an empty string to start with Ok(Self { file_path, file, content: vec![], }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { from_utf8(&self.content).expect("Invalid string!") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::str;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: vec![], }) } pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content = data.to_vec(); self.file.write_all(data)?; Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, }impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new(), }) } pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = vec![]; Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = vec![]; Ok(Self { file_path, file, content, }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") // 3. Update this method to return the content as a string slice } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type context: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, context: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.context = data.to_vec(); //self.context.extend_from_slice(data); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.context).unwrap_or("can't read buffer") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { Ok(self.read_from_cache().to_string()) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::Write;use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};use std::str::from_utf8;pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::<u8>::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content.clear(); // this is bad but the tests pass // // ideally no memory would be allocated in the cache // until a read_to_string occurs self.content.extend_from_slice(data); self.file.write_all(data)?; Ok(()) } pub fn read_from_cache(&self) -> &str { from_utf8(&self.content[..]).expect("not utf8") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { // let mut buf = String::new(); // self.file.seek(std::io::SeekFrom::Start(0))?; // self.file.read_to_string(&mut buf)?; // Ok(buf) Ok(self.read_from_cache().to_owned()) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path: file_path, file: file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = Vec::new(); Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};use std::str;pub struct TempFile<'a> { file_path: PathBuf, file: File, content: &'a [u8] }impl<'a> TempFile<'a> { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = &[]; Ok(Self { file_path, file, content }) } pub fn write(&mut self, data: &'a [u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data; Ok(()) } pub fn read_from_cache(&self) -> &str { str::from_utf8(&self.content).unwrap() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl<'a> Drop for TempFile<'a> { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: vec![] }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content = data.to_vec(); self.file.write_all(data)?; Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content = data.to_vec(); self.file.write_all(data)?; Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content = data.to_vec(); std::fs::write(&self.file_path, data); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: vec![] }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content:Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file , content:vec![] }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content.clear(); self.content.extend_from_slice(data); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,// 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = vec![]; Ok(Self { file_path, file, content}) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>,// 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = vec![]; Ok(Self { file_path, file, content}) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};use std::str;pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8>, // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content = vec![]; Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content.clear(); self.content.extend_from_slice(data); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice str::from_utf8(&self.content).unwrap() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content:Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file , content:vec![] }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content.clear(); self.content.extend_from_slice(data); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: Vec<u8> // 1. Add a new field named `content` with a valid type}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content: Vec<u8> = vec![]; Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, content: String,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: String::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content.clear(); self.content.push_str(String::from_utf8(Vec::from(data)).unwrap().as_str()); Ok(()) } pub fn read_from_cache(&self) -> &str { self.content.as_str() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: vec![] }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice return std::str::from_utf8(&self.content).unwrap() } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; Ok(Self { file_path, file, content: Vec::new() }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.file.write_all(data)?; self.content = data.to_vec(); Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content) .unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}
use std::fs::{remove_file, File, OpenOptions};use std::io::{Read, Seek, Write};use std::path::PathBuf;use std::time::{SystemTime, UNIX_EPOCH};pub struct TempFile { file_path: PathBuf, file: File, // 1. Add a new field named `content` with a valid type content: Vec<u8>,}impl TempFile { pub fn new() -> Result<Self, std::io::Error> { let temp_dir = std::env::temp_dir(); let random_number: u64 = { let start = SystemTime::now(); let since_epoch = start.duration_since(UNIX_EPOCH).unwrap(); since_epoch.as_nanos() as u64 }; let file_name = format!("tempfile-{}.tmp", random_number); let file_path = temp_dir.join(file_name); let file = OpenOptions::new() .read(true) .write(true) .create(true) .open(&file_path)?; let content: Vec<u8> = Vec::new(); Ok(Self { file_path, file, content }) } // 2. Change this method to update the `content` field on every write pub fn write(&mut self, data: &[u8]) -> Result<(), std::io::Error> { self.content = data.to_vec(); self.file.write_all(data)?; Ok(()) } pub fn read_from_cache(&self) -> &str { // 3. Update this method to return the content as a string slice std::str::from_utf8(&self.content).unwrap_or("") } pub fn read_to_string(&mut self) -> Result<String, std::io::Error> { let mut buf = String::new(); self.file.seek(std::io::SeekFrom::Start(0))?; self.file.read_to_string(&mut buf)?; Ok(buf) } pub fn path(&self) -> &PathBuf { &self.file_path } pub fn file(&self) -> &File { &self.file }}impl Drop for TempFile { fn drop(&mut self) { let _ = remove_file(&self.file_path); }}