mysql_common/proto/mod.rs
1// Copyright (c) 2017 Anatoly Ikorsky
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use std::io;
10
11use crate::io::ParseBuf;
12
13pub mod codec;
14pub mod sync_framed;
15
16/// Text protocol marker.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18pub struct Text;
19
20/// Binary protocol marker.
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub struct Binary;
23
24/// Serialization for various MySql types.
25pub trait MySerialize {
26 /// Serializes self into the `buf`.
27 fn serialize(&self, buf: &mut Vec<u8>);
28}
29
30/// Deserialization for various MySql types.
31pub trait MyDeserialize<'de>: Sized {
32 /// Size hint of a serialized value (in bytes), if it's constant.
33 const SIZE: Option<usize>;
34
35 /// Some structs defines deserialization in the context of another value.
36 ///
37 /// Use `()` here if the deserialization procedure is defined without premises.
38 type Ctx;
39
40 /// Deserializes self from the given `buf`.
41 ///
42 /// Imlementation must consume corresponding amount of bytes from the `buf`.
43 ///
44 /// # Panic
45 ///
46 /// Implementation must panic on insufficient buffer length if `Self::SIZE.is_some()`.
47 /// One should use `ParseBuf::checked_parse` for checked deserialization.
48 fn deserialize(ctx: Self::Ctx, buf: &mut ParseBuf<'de>) -> io::Result<Self>;
49}