Skip to content

Commit 8516d84

Browse files
committed
Tweak connection pool API
1 parent a1acc98 commit 8516d84

File tree

3 files changed

+15
-20
lines changed

3 files changed

+15
-20
lines changed

src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ impl InnerPostgresConnection {
414414
host,
415415
port,
416416
user,
417-
mut path,
417+
path,
418418
query: mut args,
419419
..
420420
}: Url = match FromStr::from_str(url) {
@@ -454,8 +454,10 @@ impl InnerPostgresConnection {
454454
args.push((~"user", user.user.clone()));
455455
if !path.is_empty() {
456456
// path contains the leading /
457-
path.shift_char();
458-
args.push((~"database", path));
457+
let mut path = StrBuf::from_owned_str(path);
458+
// FIXME
459+
unsafe { path.shift_byte(); }
460+
args.push((~"database", path.into_owned()));
459461
}
460462
try_pg_conn!(conn.write_messages([StartupMessage {
461463
version: message::PROTOCOL_VERSION,

src/pool.rs

+9-16
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ impl Drop for InnerConnectionPool {
2525
loop {
2626
match self.pool.pop() {
2727
Some(conn) => unsafe {
28-
drop(cast::transmute::<*(), ~PostgresConnection>(conn));
28+
let c: ~PostgresConnection = cast::transmute(conn);
29+
drop(c);
2930
},
3031
None => break
3132
}
@@ -34,14 +35,9 @@ impl Drop for InnerConnectionPool {
3435
}
3536

3637
impl InnerConnectionPool {
37-
fn new_connection(&mut self) -> Option<PostgresConnectError> {
38-
match PostgresConnection::connect(self.url, &self.ssl) {
39-
Ok(conn) => {
40-
unsafe { self.pool.push(cast::transmute(~conn)) };
41-
None
42-
}
43-
Err(err) => Some(err)
44-
}
38+
fn add_connection(&mut self) -> Result<(), PostgresConnectError> {
39+
PostgresConnection::connect(self.url, &self.ssl)
40+
.map(|c| unsafe { self.pool.push(cast::transmute(~c)); })
4541
}
4642
}
4743

@@ -54,7 +50,7 @@ impl InnerConnectionPool {
5450
/// ```rust,no_run
5551
/// # use postgres::NoSsl;
5652
/// # use postgres::pool::PostgresConnectionPool;
57-
/// let pool = PostgresConnectionPool::new("postgres://postgres@localhost",
53+
/// let pool = PostgresConnectionPool::new(~"postgres://postgres@localhost",
5854
/// NoSsl, 5).unwrap();
5955
/// for _ in range(0, 10) {
6056
/// let pool = pool.clone();
@@ -74,19 +70,16 @@ impl PostgresConnectionPool {
7470
///
7571
/// Returns an error if the specified number of connections cannot be
7672
/// created.
77-
pub fn new(url: &str, ssl: SslMode, pool_size: uint)
73+
pub fn new(url: ~str, ssl: SslMode, pool_size: uint)
7874
-> Result<PostgresConnectionPool, PostgresConnectError> {
7975
let mut pool = InnerConnectionPool {
80-
url: url.to_owned(),
76+
url: url,
8177
ssl: ssl,
8278
pool: Vec::new(),
8379
};
8480

8581
for _ in range(0, pool_size) {
86-
match pool.new_connection() {
87-
None => (),
88-
Some(err) => return Err(err)
89-
}
82+
try!(pool.add_connection());
9083
}
9184

9285
Ok(PostgresConnectionPool {

src/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ macro_rules! or_fail(
4949
#[test]
5050
// Make sure we can take both connections at once and can still get one after
5151
fn test_pool() {
52-
let pool = or_fail!(PostgresConnectionPool::new("postgres://postgres@localhost",
52+
let pool = or_fail!(PostgresConnectionPool::new(~"postgres://postgres@localhost",
5353
NoSsl, 2));
5454

5555
let (stream1, stream2) = sync::duplex();

0 commit comments

Comments
 (0)