pub struct Client<T: Serialize + DeserializeOwned> { /* private fields */ }Expand description
An object that is responsible for handling IO operations with regards to file opening/closing/writing as well as serialization and deserialization. The main data type of this crate.
Implementations
sourceimpl<T> Client<T> where
T: Serialize + DeserializeOwned,
impl<T> Client<T> where
T: Serialize + DeserializeOwned,
sourcepub fn new<P: AsRef<Path>>(
path: P,
append: bool
) -> Result<Self, DatabaseError<T>>
pub fn new<P: AsRef<Path>>(
path: P,
append: bool
) -> Result<Self, DatabaseError<T>>
Creates a new client object. It opens a file if a file with the same name exists or
creates a new file if it doesn’t exist. Set the append parameter to false if you want to
overwrite all data while calling write() or write_many(), or true if you
simply want to append data to the file.
Errors
- The usual
std::io::Errorif it fails to open or create a new file.
sourcepub fn load(&mut self) -> Result<Option<Vec<T>>, DatabaseError<T>>
pub fn load(&mut self) -> Result<Option<Vec<T>>, DatabaseError<T>>
Returns a vector of the deserialized object. If the file is empty, this method
returns Ok(None).
Errors
-
If a checksum mismatch occurs, a
DataPoisonError<T>is returned, in which you can get the underlying deserialized objects via the methodinto_inner(). -
bincode::Erroroccurs if the deserializer fails to deserialize bytes from the file to your requested object. In that case, the most probable reason is that the data in that file stores some other data type and you are attempting to deserialize it to the wrong data type. -
The usual
std::io::Errorsuch asErrorKind::UnexpectedEofif the file that is being accessed is malformed and there are no more bytes to be read when the method is expecting more data.
sourcepub fn write_many(&mut self, data: &[T]) -> Result<(), DatabaseError<T>>
pub fn write_many(&mut self, data: &[T]) -> Result<(), DatabaseError<T>>
Writes the provided serializable objects to disk. If no file is found, a new file will be created and written to.
Errors
-
std::num::TryFromIntErroroccurs when an object you are inserting takes up more space thanu32::MAXbytes. In that case, seek help. -
The usual
std::io::Errorsuch asErrorKind::UnexpectedEofif the file that is being accessed is malformed and there are no more bytes to be read when the method is expecting more data. -
Serialization errors when the data provided fails to serialize for some reason.
sourcepub fn write(&mut self, data: &T) -> Result<(), DatabaseError<T>>
pub fn write(&mut self, data: &T) -> Result<(), DatabaseError<T>>
Writes the provided serializable object to disk. If no file is found, a new file will be created and written to.
Errors
-
DatabaseError<T>::DataTooLargeoccurs when an object you are inserting takes up more space thanu32::MAXbytes. In that case, seek help. -
The usual
std::io::Errorsuch asErrorKind::UnexpectedEofif the file that is being accessed is malformed and there are no more bytes to be read when the method is expecting more data.
Auto Trait Implementations
impl<T> RefUnwindSafe for Client<T> where
T: RefUnwindSafe,
impl<T> Send for Client<T> where
T: Send,
impl<T> Sync for Client<T> where
T: Sync,
impl<T> Unpin for Client<T> where
T: Unpin,
impl<T> UnwindSafe for Client<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more