Skip to content

Commit 4f04341

Browse files
Use T: AsRef<Path> as param to SqliteDatabase::new
Currently SqliteDatabase::new takes a String as path, with this change, it now accepts any type that implements AsRef<Path>.
1 parent 277e18f commit 4f04341

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88

99
- Add `descriptor::checksum::get_checksum_bytes` method.
10+
- Change the interface of `SqliteDatabase::new` to accept any type that implement AsRef<Path>
1011

1112
## [v0.20.0] - [v0.19.0]
1213

src/database/sqlite.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
99
// You may not use this file except in accordance with one or both of these
1010
// licenses.
11+
use std::path::Path;
12+
use std::path::PathBuf;
1113

1214
use bitcoin::consensus::encode::{deserialize, serialize};
1315
use bitcoin::hash_types::Txid;
@@ -60,17 +62,20 @@ static MIGRATIONS: &[&str] = &[
6062
#[derive(Debug)]
6163
pub struct SqliteDatabase {
6264
/// Path on the local filesystem to store the sqlite file
63-
pub path: String,
65+
pub path: PathBuf,
6466
/// A rusqlite connection object to the sqlite database
6567
pub connection: Connection,
6668
}
6769

6870
impl SqliteDatabase {
6971
/// Instantiate a new SqliteDatabase instance by creating a connection
7072
/// to the database stored at path
71-
pub fn new(path: String) -> Self {
73+
pub fn new<T: AsRef<Path>>(path: T) -> Self {
7274
let connection = get_connection(&path).unwrap();
73-
SqliteDatabase { path, connection }
75+
SqliteDatabase {
76+
path: PathBuf::from(path.as_ref()),
77+
connection,
78+
}
7479
}
7580
fn insert_script_pubkey(
7681
&self,
@@ -908,7 +913,7 @@ impl BatchDatabase for SqliteDatabase {
908913
}
909914
}
910915

911-
pub fn get_connection(path: &str) -> Result<Connection, Error> {
916+
pub fn get_connection<T: AsRef<Path>>(path: &T) -> Result<Connection, Error> {
912917
let connection = Connection::open(path)?;
913918
migrate(&connection)?;
914919
Ok(connection)

0 commit comments

Comments
 (0)