From 558e37afa72b69228fc40cc20be9122b3ea00597 Mon Sep 17 00:00:00 2001 From: Vladimir Fomene Date: Wed, 20 Jul 2022 17:31:17 +0100 Subject: [PATCH] Use T: AsRef as param to SqliteDatabase::new Currently SqliteDatabase::new takes a String as path, with this change, it now accepts any type that implements AsRef. --- CHANGELOG.md | 1 + src/database/sqlite.rs | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0b2959f8..e6511bdc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Add `descriptor::checksum::get_checksum_bytes` method. - Add `Excess` enum to handle remaining amount after coin selection. - Move change creation from `Wallet::create_tx` to `CoinSelectionAlgorithm::coin_select`. +- Change the interface of `SqliteDatabase::new` to accept any type that implement AsRef ## [v0.20.0] - [v0.19.0] diff --git a/src/database/sqlite.rs b/src/database/sqlite.rs index 06ed7e43c..dc35b856c 100644 --- a/src/database/sqlite.rs +++ b/src/database/sqlite.rs @@ -8,6 +8,8 @@ // , at your option. // You may not use this file except in accordance with one or both of these // licenses. +use std::path::Path; +use std::path::PathBuf; use bitcoin::consensus::encode::{deserialize, serialize}; use bitcoin::hash_types::Txid; @@ -60,7 +62,7 @@ static MIGRATIONS: &[&str] = &[ #[derive(Debug)] pub struct SqliteDatabase { /// Path on the local filesystem to store the sqlite file - pub path: String, + pub path: PathBuf, /// A rusqlite connection object to the sqlite database pub connection: Connection, } @@ -68,9 +70,12 @@ pub struct SqliteDatabase { impl SqliteDatabase { /// Instantiate a new SqliteDatabase instance by creating a connection /// to the database stored at path - pub fn new(path: String) -> Self { + pub fn new>(path: T) -> Self { let connection = get_connection(&path).unwrap(); - SqliteDatabase { path, connection } + SqliteDatabase { + path: PathBuf::from(path.as_ref()), + connection, + } } fn insert_script_pubkey( &self, @@ -908,7 +913,7 @@ impl BatchDatabase for SqliteDatabase { } } -pub fn get_connection(path: &str) -> Result { +pub fn get_connection>(path: &T) -> Result { let connection = Connection::open(path)?; migrate(&connection)?; Ok(connection)