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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::ffi::CStr;
use sys;
#[derive(Debug)]
pub struct SBStream {
pub raw: sys::SBStreamRef,
}
impl SBStream {
pub fn new() -> SBStream {
SBStream::wrap(unsafe { sys::CreateSBStream() })
}
pub fn wrap(raw: sys::SBStreamRef) -> SBStream {
SBStream { raw: raw }
}
pub fn maybe_wrap(raw: sys::SBStreamRef) -> Option<SBStream> {
if unsafe { sys::SBStreamIsValid(raw) != 0 } {
Some(SBStream { raw: raw })
} else {
None
}
}
pub fn is_valid(&self) -> bool {
unsafe { sys::SBStreamIsValid(self.raw) != 0 }
}
pub fn clear(&self) {
unsafe { sys::SBStreamClear(self.raw) }
}
pub fn data(&self) -> &str {
unsafe {
match CStr::from_ptr(sys::SBStreamGetData(self.raw)).to_str() {
Ok(s) => s,
_ => panic!("Invalid string?"),
}
}
}
pub fn len(&self) -> usize {
unsafe { sys::SBStreamGetSize(self.raw) as usize }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl Default for SBStream {
fn default() -> SBStream {
SBStream::new()
}
}
impl Drop for SBStream {
fn drop(&mut self) {
unsafe { sys::DisposeSBStream(self.raw) };
}
}