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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::ffi::{CStr, CString};
use std::fmt;
use curl_sys;
use Error;
pub struct List {
raw: *mut curl_sys::curl_slist,
}
#[derive(Clone)]
pub struct Iter<'a> {
_me: &'a List,
cur: *mut curl_sys::curl_slist,
}
pub fn raw(list: &List) -> *mut curl_sys::curl_slist {
list.raw
}
pub unsafe fn from_raw(raw: *mut curl_sys::curl_slist) -> List {
List { raw: raw }
}
unsafe impl Send for List {}
impl List {
pub fn new() -> List {
List { raw: 0 as *mut _ }
}
pub fn append(&mut self, data: &str) -> Result<(), Error> {
let data = CString::new(data)?;
unsafe {
let raw = curl_sys::curl_slist_append(self.raw, data.as_ptr());
assert!(!raw.is_null());
self.raw = raw;
Ok(())
}
}
pub fn iter(&self) -> Iter {
Iter {
_me: self,
cur: self.raw,
}
}
}
impl fmt::Debug for List {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list()
.entries(self.iter().map(String::from_utf8_lossy))
.finish()
}
}
impl<'a> IntoIterator for &'a List {
type IntoIter = Iter<'a>;
type Item = &'a [u8];
fn into_iter(self) -> Iter<'a> {
self.iter()
}
}
impl Drop for List {
fn drop(&mut self) {
unsafe { curl_sys::curl_slist_free_all(self.raw) }
}
}
impl<'a> Iterator for Iter<'a> {
type Item = &'a [u8];
fn next(&mut self) -> Option<&'a [u8]> {
if self.cur.is_null() {
return None;
}
unsafe {
let ret = Some(CStr::from_ptr((*self.cur).data).to_bytes());
self.cur = (*self.cur).next;
return ret;
}
}
}
impl<'a> fmt::Debug for Iter<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list()
.entries(self.clone().map(String::from_utf8_lossy))
.finish()
}
}