-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathty.rs
More file actions
65 lines (54 loc) · 1.72 KB
/
Copy pathty.rs
File metadata and controls
65 lines (54 loc) · 1.72 KB
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
// Column type
use std::slice;
use std::io::{Write};
use byteorder::{WriteBytesExt, ReadBytesExt};
use catalog::mini_attribute::{TypeLabel, ty_byte_len};
pub trait TypeValue {
fn write_bytes(&self, wrt: &mut Write) -> std::io::Result<()>;
fn len(&self) -> u32;
fn as_string(&self) -> String;
fn as_pointer(&self) -> *const libc::c_void;
}
// This function transforms data in database to TypeValue, so use methods of
// byteorder crate.
pub fn load_type_value(tl: &TypeLabel, src: *const libc::c_void) -> Box<TypeValue> {
let len = ty_byte_len(tl);
match tl {
TypeLabel::Integer => {
let ptr: *const u8 = src as *const u8;
let mut s = unsafe { slice::from_raw_parts(ptr, len as usize) };
let i = s.read_i32::<byteorder::LittleEndian>().unwrap();
Box::new(Integer { elem: i })
}
}
}
// This function transforms input from user, mainly SQL, to
// TypeValue, so use `parse` method. We can call `unwrap` because
// `row` will be passed parser syntax check.
pub fn build_type_value(tl: &TypeLabel, row: &str) -> Box<TypeValue> {
match tl {
TypeLabel::Integer => {
let elem = row.parse::<i32>().unwrap();
Box::new(Integer { elem: elem })
}
}
}
// Signed 4 bytes integer
pub struct Integer {
pub elem: i32,
}
impl TypeValue for Integer {
fn write_bytes(&self, wrt: &mut Write) -> std::io::Result<()> {
wrt.write_i32::<byteorder::LittleEndian>(self.elem)
}
fn len(&self) -> u32 {
4
}
fn as_string(&self) -> String {
self.elem.to_string()
}
fn as_pointer(&self) -> *const libc::c_void {
let p: *const i32 = &self.elem;
p as *const libc::c_void
}
}