Skip to content

Commit a286575

Browse files
committed
fix reading/saving issues
1 parent b9b41f9 commit a286575

File tree

4 files changed

+43
-44
lines changed

4 files changed

+43
-44
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "draw-together"
3-
version = "2.2.2"
3+
version = "2.2.3"
44
edition = "2021"
55

66
[[bin]]

src/data.rs

+39-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::sync::Arc;
1+
use std::{path::Path, sync::Arc};
22
use tokio::{
3-
fs::{File, OpenOptions},
3+
fs::File,
44
io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt},
55
sync::Mutex,
66
};
@@ -136,57 +136,53 @@ pub struct Data {
136136
}
137137

138138
impl Data {
139-
pub async fn new(path: Option<&str>, save: bool) -> Self {
140-
let mut file = match path {
141-
Some(path) => Some(if save {
142-
OpenOptions::new()
143-
.read(true)
144-
.write(true)
145-
.create(true)
146-
.truncate(true)
147-
.open(path)
148-
.await
149-
.unwrap()
150-
} else {
151-
File::open(path).await.unwrap()
152-
}),
139+
pub async fn new(path: Option<String>, save: bool) -> Self {
140+
let mut file = match path.clone() {
141+
Some(path) => match Path::new(&path).exists() {
142+
true => Some(File::open(path).await.unwrap()),
143+
false => None,
144+
},
153145
None => None,
154146
};
155147

156148
let mut data: Vec<u8> = vec![0xff; RESOLUTION * 3];
157149
if file.is_some() {
150+
data.clear();
158151
file.as_mut().unwrap().read_to_end(&mut data).await.unwrap();
159152
}
160153

154+
drop(file);
155+
161156
let data = Arc::new(Mutex::new(data));
162157
let task_data = Arc::clone(&data);
163-
if file.is_some() && save {
164-
tokio::spawn(async move {
165-
#[allow(clippy::unnecessary_unwrap)]
166-
let mut file = file.unwrap();
158+
if let Some(path) = path {
159+
if save {
160+
tokio::spawn(async move {
161+
let mut file = File::options()
162+
.write(true)
163+
.truncate(true)
164+
.create(true)
165+
.open(path)
166+
.await
167+
.unwrap();
167168

168-
loop {
169-
tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;
169+
loop {
170+
tokio::time::sleep(tokio::time::Duration::from_secs(10)).await;
170171

171-
println!("saving data...");
172+
println!("saving data...");
172173

173-
file.seek(std::io::SeekFrom::Start(0)).await.unwrap();
174+
file.seek(tokio::io::SeekFrom::Start(0)).await.unwrap();
174175

175-
let data = task_data.lock().await;
176-
file.write_all(
177-
&data
178-
.iter()
179-
.flat_map(|data| data.to_le_bytes())
180-
.collect::<Vec<u8>>(),
181-
)
182-
.await
183-
.unwrap();
176+
let data = task_data.lock().await;
177+
file.write_all(&data).await.unwrap();
184178

185-
println!("saving data... done");
179+
drop(data);
180+
file.sync_all().await.unwrap();
186181

187-
file.sync_all().await.unwrap();
188-
}
189-
});
182+
println!("saving data... done");
183+
}
184+
});
185+
}
190186
}
191187

192188
Self {
@@ -231,7 +227,7 @@ impl Data {
231227
}
232228
Action::DrawCubeNormal => {
233229
let height = message.height as usize * 4;
234-
println!("height: {}", height);
230+
235231
let start_x = message.x as usize;
236232
let end_x =
237233
((message.x + height as u16).min(RESOLUTION_WIDTH as u16 - 1)) as usize;
@@ -249,6 +245,7 @@ impl Data {
249245
}
250246
Action::DrawCubeHollow => {
251247
let height = message.height as usize * 4;
248+
252249
let start_x = message.x as usize;
253250
let end_x =
254251
((message.x + height as u16).min(RESOLUTION_WIDTH as u16 - 1)) as usize;
@@ -343,9 +340,11 @@ impl Data {
343340
let mut encoded = Vec::with_capacity(7 * data.len());
344341
encoded.extend(data.iter().flat_map(|msg| msg.encode()));
345342

346-
self.listeners.retain(|listener| !listener.is_closed());
347-
348343
for listener in &self.listeners {
344+
if listener.is_closed() {
345+
continue;
346+
}
347+
349348
listener.send(encoded.clone()).await.unwrap();
350349
}
351350
}

src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ async fn main() {
2929
let data = Arc::new(Mutex::new(
3030
data::Data::new(
3131
match Path::new("history_2.raw").exists() {
32-
true => Some("history_2.raw"),
32+
true => Some("history_2.raw".to_string()),
3333
false => match nosave {
3434
true => None,
35-
false => Some("history_2.raw"),
35+
false => Some("history_2.raw".to_string()),
3636
},
3737
},
3838
!nosave,

0 commit comments

Comments
 (0)