1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! This module defines the `DataWriterTrait` trait for writing data to various destinations.
//!
//! # Overview
//!
//! The `DataWriterTrait` trait provides an interface for writing data to different destinations.
//! Implementations of this trait can append data, write data from the start, and manage the write position.
//! This trait is designed to be implemented by any struct that handles data writing operations.
//!
//! # Examples
//!
//! ```rust
//! use versatiles_core::{io::DataWriterTrait, Blob, ByteRange};
//! use anyhow::Result;
//!
//! struct MockDataWriter {
//! data: Vec<u8>,
//! position: u64,
//! }
//!
//! impl DataWriterTrait for MockDataWriter {
//! fn append(&mut self, blob: &Blob) -> Result<ByteRange> {
//! let pos = self.position;
//! self.data.extend_from_slice(blob.as_slice());
//! self.position += blob.len() as u64;
//! Ok(ByteRange::new(pos, blob.len() as u64))
//! }
//!
//! fn write_start(&mut self, blob: &Blob) -> Result<()> {
//! self.data.splice(0..blob.len() as usize, blob.as_slice().iter().cloned());
//! Ok(())
//! }
//!
//! fn get_position(&mut self) -> Result<u64> {
//! Ok(self.position)
//! }
//!
//! fn set_position(&mut self, position: u64) -> Result<()> {
//! self.position = position;
//! Ok(())
//! }
//! }
//!
//! fn main() -> Result<()> {
//! let mut writer = MockDataWriter { data: vec![], position: 0 };
//! let data = Blob::from(vec![1, 2, 3, 4]);
//!
//! // Appending data
//! let range = writer.append(&data)?;
//! assert_eq!(range, ByteRange::new(0, 4));
//!
//! // Writing data from the start
//! writer.write_start(&Blob::from(vec![5, 6, 7, 8]))?;
//! assert_eq!(writer.data, vec![5, 6, 7, 8]);
//!
//! Ok(())
//! }
//! ```
use crate::;
use Result;
/// A trait for writing data to various destinations.
///
/// # Required Methods
/// - `append`: Appends data to the writer.
/// - `write_start`: Writes data from the start of the writer.
/// - `get_position`: Gets the current write position.
/// - `set_position`: Sets the write position.