fud/scrap.rs
1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use darkfi_serial::{async_trait, AsyncDecodable, AsyncEncodable, AsyncRead, AsyncWrite};
20use std::io::Result;
21
22#[derive(Clone, Debug)]
23pub struct Scrap {
24 pub chunk: Vec<u8>,
25 /// Hash of the data that was last written to the file system (parts of `chunk`).
26 /// Used to check if the data on the filesystem changed and the scrap should be rewritten.
27 pub hash_written: blake3::Hash,
28}
29
30#[async_trait]
31impl AsyncEncodable for Scrap {
32 #[inline]
33 async fn encode_async<S: AsyncWrite + Unpin + Send>(&self, s: &mut S) -> Result<usize> {
34 let mut len = 0;
35 len += self.chunk.encode_async(s).await?;
36 len += self.hash_written.encode_async(s).await?;
37 Ok(len)
38 }
39}
40
41#[async_trait]
42impl AsyncDecodable for Scrap {
43 async fn decode_async<D: AsyncRead + Unpin + Send>(d: &mut D) -> Result<Self> {
44 Ok(Self {
45 chunk: <Vec<u8>>::decode_async(d).await?,
46 hash_written: blake3::Hash::decode_async(d).await?,
47 })
48 }
49}