dsi_bitstream/traits/
words.rs

1/*
2 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
3 * SPDX-FileCopyrightText: 2023 Inria
4 * SPDX-FileCopyrightText: 2023 Sebastiano Vigna
5 *
6 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
7 */
8
9use std::error::Error;
10
11use common_traits::*;
12
13/// This is a trait alias for all the properties that we need words of memory
14/// read and wrote by either a [`WordRead`] or [`WordWrite`], respectively.
15pub trait Word: UnsignedInt + ToBytes + FromBytes + FiniteRangeNumber {}
16impl<W: UnsignedInt + ToBytes + FromBytes + FiniteRangeNumber> Word for W {}
17
18/// Sequential, streaming word-by-word reads.
19pub trait WordRead {
20    type Error: Error + Send + Sync + 'static;
21
22    /// The word type (the type of the result of [`WordRead::read_word`]).
23    type Word: Word;
24
25    /// Read a word and advance the current position.
26    fn read_word(&mut self) -> Result<Self::Word, Self::Error>;
27}
28
29/// Sequential, streaming word-by-word writes.
30pub trait WordWrite {
31    type Error: Error + Send + Sync + 'static;
32
33    /// The word type (the type of the argument of [`WordWrite::write_word`]).
34    type Word: Word;
35
36    /// Write a word and advance the current position.
37    fn write_word(&mut self, word: Self::Word) -> Result<(), Self::Error>;
38
39    /// Flush the stream.
40    fn flush(&mut self) -> Result<(), Self::Error>;
41}
42
43/// Seekability for [`WordRead`] and [`WordWrite`] streams.
44pub trait WordSeek {
45    type Error: Error + Send + Sync + 'static;
46    /// Get the current position in words from the start of the file.
47    fn word_pos(&mut self) -> Result<u64, Self::Error>;
48
49    /// Set the current position in words from the start of the file to `word_pos`.
50    fn set_word_pos(&mut self, word_pos: u64) -> Result<(), Self::Error>;
51}