cassandra_proto/frame/
traits.rs

1use std::io::Cursor;
2
3use crate::error;
4use crate::types::rows::Row;
5use crate::types::udt::UDT;
6use crate::query;
7
8/// `IntoBytes` should be used to convert a structure into array of bytes.
9pub trait IntoBytes {
10  /// It should convert a struct into an array of bytes.
11  fn into_cbytes(&self) -> Vec<u8>;
12}
13
14/// `FromBytes` should be used to parse an array of bytes into a structure.
15pub trait FromBytes {
16  /// It gets and array of bytes and should return an implementor struct.
17  fn from_bytes(bytes: &[u8]) -> error::Result<Self>
18  where
19    Self: Sized;
20}
21
22/// `AsBytes` should be used to convert a value into a single byte.
23pub trait AsByte {
24  /// It should represent a struct as a single byte.
25  fn as_byte(&self) -> u8;
26}
27
28/// `FromSingleByte` should be used to convert a single byte into a value.
29/// It is opposite to `AsByte`.
30pub trait FromSingleByte {
31  /// It should convert a single byte into an implementor struct.
32  fn from_byte(byte: u8) -> Self;
33}
34
35/// `FromCursor` should be used to get parsed structure from an `io:Cursor`
36/// wich bound to an array of bytes.
37pub trait FromCursor {
38  /// It should return an implementor from an `io::Cursor` over an array of bytes.
39  fn from_cursor(cursor: &mut Cursor<&[u8]>) -> error::Result<Self>
40  where
41    Self: Sized;
42}
43
44/// The trait that allows transformation of `Self` to CDRS query values.
45pub trait IntoQueryValues {
46  fn into_query_values(self) -> query::QueryValues;
47}
48
49pub trait TryFromRow: Sized {
50  fn try_from_row(row: Row) -> error::Result<Self>;
51}
52
53pub trait TryFromUDT: Sized {
54  fn try_from_udt(udt: UDT) -> error::Result<Self>;
55}