9
9
//!
10
10
//! ```no_run
11
11
//! # use hyper::Client;
12
- //! let mut client = Client::new();
12
+ //! let client = Client::new();
13
13
//!
14
14
//! let res = client.get("http://example.domain").send().unwrap();
15
15
//! assert_eq!(res.status, hyper::Ok);
23
23
//!
24
24
//! ```no_run
25
25
//! # use hyper::Client;
26
- //! let mut client = Client::new();
26
+ //! let client = Client::new();
27
27
//!
28
28
//! let res = client.post("http://example.domain")
29
29
//! .body("foo=bar")
30
30
//! .send()
31
31
//! .unwrap();
32
32
//! assert_eq!(res.status, hyper::Ok);
33
33
//! ```
34
+ //!
35
+ //! # Sync
36
+ //!
37
+ //! The `Client` implements `Sync`, so you can share it among multiple threads
38
+ //! and make multiple requests simultaneously.
39
+ //!
40
+ //! ```no_run
41
+ //! # use hyper::Client;
42
+ //! use std::sync::Arc;
43
+ //! use std::thread;
44
+ //!
45
+ //! // Note: an Arc is used here because `thread::spawn` creates threads that
46
+ //! // can outlive the main thread, so we must use reference counting to keep
47
+ //! // the Client alive long enough. Scoped threads could skip the Arc.
48
+ //! let client = Arc::new(Client::new());
49
+ //! let clone1 = client.clone();
50
+ //! let clone2 = client.clone();
51
+ //! thread::spawn(move || {
52
+ //! clone1.get("http://example.domain").send().unwrap();
53
+ //! });
54
+ //! thread::spawn(move || {
55
+ //! clone2.post("http://example.domain/post").body("foo=bar").send().unwrap();
56
+ //! });
57
+ //! ```
34
58
use std:: default:: Default ;
35
59
use std:: io:: { self , copy, Read } ;
36
60
use std:: iter:: Extend ;
@@ -61,7 +85,7 @@ use http::h1::Http11Protocol;
61
85
///
62
86
/// Clients can handle things such as: redirect policy, connection pooling.
63
87
pub struct Client {
64
- protocol : Box < Protocol + Send > ,
88
+ protocol : Box < Protocol + Send + Sync > ,
65
89
redirect_policy : RedirectPolicy ,
66
90
}
67
91
@@ -79,12 +103,12 @@ impl Client {
79
103
80
104
/// Create a new client with a specific connector.
81
105
pub fn with_connector < C , S > ( connector : C ) -> Client
82
- where C : NetworkConnector < Stream =S > + Send + ' static , S : NetworkStream + Send {
106
+ where C : NetworkConnector < Stream =S > + Send + Sync + ' static , S : NetworkStream + Send {
83
107
Client :: with_protocol ( Http11Protocol :: with_connector ( connector) )
84
108
}
85
109
86
110
/// Create a new client with a specific `Protocol`.
87
- pub fn with_protocol < P : Protocol + Send + ' static > ( protocol : P ) -> Client {
111
+ pub fn with_protocol < P : Protocol + Send + Sync + ' static > ( protocol : P ) -> Client {
88
112
Client {
89
113
protocol : Box :: new ( protocol) ,
90
114
redirect_policy : Default :: default ( )
@@ -102,33 +126,33 @@ impl Client {
102
126
}
103
127
104
128
/// Build a Get request.
105
- pub fn get < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
129
+ pub fn get < U : IntoUrl > ( & self , url : U ) -> RequestBuilder < U > {
106
130
self . request ( Method :: Get , url)
107
131
}
108
132
109
133
/// Build a Head request.
110
- pub fn head < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
134
+ pub fn head < U : IntoUrl > ( & self , url : U ) -> RequestBuilder < U > {
111
135
self . request ( Method :: Head , url)
112
136
}
113
137
114
138
/// Build a Post request.
115
- pub fn post < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
139
+ pub fn post < U : IntoUrl > ( & self , url : U ) -> RequestBuilder < U > {
116
140
self . request ( Method :: Post , url)
117
141
}
118
142
119
143
/// Build a Put request.
120
- pub fn put < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
144
+ pub fn put < U : IntoUrl > ( & self , url : U ) -> RequestBuilder < U > {
121
145
self . request ( Method :: Put , url)
122
146
}
123
147
124
148
/// Build a Delete request.
125
- pub fn delete < U : IntoUrl > ( & mut self , url : U ) -> RequestBuilder < U > {
149
+ pub fn delete < U : IntoUrl > ( & self , url : U ) -> RequestBuilder < U > {
126
150
self . request ( Method :: Delete , url)
127
151
}
128
152
129
153
130
154
/// Build a new request using this Client.
131
- pub fn request < U : IntoUrl > ( & mut self , method : Method , url : U ) -> RequestBuilder < U > {
155
+ pub fn request < U : IntoUrl > ( & self , method : Method , url : U ) -> RequestBuilder < U > {
132
156
RequestBuilder {
133
157
client : self ,
134
158
method : method,
0 commit comments