diff --git a/crates/duckdb/src/arrow_batch.rs b/crates/duckdb/src/arrow_batch.rs index 8588ad7e..f496d6df 100644 --- a/crates/duckdb/src/arrow_batch.rs +++ b/crates/duckdb/src/arrow_batch.rs @@ -12,7 +12,7 @@ pub struct Arrow<'stmt> { #[allow(clippy::needless_lifetimes)] impl<'stmt> Arrow<'stmt> { #[inline] - pub(crate) fn new(stmt: &'stmt Statement<'stmt>) -> Arrow<'stmt> { + pub(crate) fn new(stmt: &'stmt Statement<'stmt>) -> Self { Arrow { stmt: Some(stmt) } } @@ -43,7 +43,7 @@ pub struct ArrowStream<'stmt> { #[allow(clippy::needless_lifetimes)] impl<'stmt> ArrowStream<'stmt> { #[inline] - pub(crate) fn new(stmt: &'stmt Statement<'stmt>, schema: SchemaRef) -> ArrowStream<'stmt> { + pub(crate) fn new(stmt: &'stmt Statement<'stmt>, schema: SchemaRef) -> Self { ArrowStream { stmt: Some(stmt), schema, diff --git a/crates/duckdb/src/cache.rs b/crates/duckdb/src/cache.rs index cd2d44eb..467b4df5 100644 --- a/crates/duckdb/src/cache.rs +++ b/crates/duckdb/src/cache.rs @@ -120,8 +120,8 @@ impl CachedStatement<'_> { impl StatementCache { /// Create a statement cache. #[inline] - pub fn with_capacity(capacity: usize) -> StatementCache { - StatementCache(RefCell::new(LruCache::new(capacity))) + pub fn with_capacity(capacity: usize) -> Self { + Self(RefCell::new(LruCache::new(capacity))) } #[inline] diff --git a/crates/duckdb/src/config.rs b/crates/duckdb/src/config.rs index 3fe68904..d9acc404 100644 --- a/crates/duckdb/src/config.rs +++ b/crates/duckdb/src/config.rs @@ -53,69 +53,69 @@ impl Config { } /// enable autoload extensions - pub fn enable_autoload_extension(mut self, enabled: bool) -> Result { + pub fn enable_autoload_extension(mut self, enabled: bool) -> Result { self.set("autoinstall_known_extensions", &(enabled as i32).to_string())?; self.set("autoload_known_extensions", &(enabled as i32).to_string())?; Ok(self) } /// Access mode of the database ([AUTOMATIC], READ_ONLY or READ_WRITE) - pub fn access_mode(mut self, mode: AccessMode) -> Result { + pub fn access_mode(mut self, mode: AccessMode) -> Result { self.set("access_mode", &mode.to_string())?; Ok(self) } /// Metadata from DuckDB callers - pub fn custom_user_agent(mut self, custom_user_agent: &str) -> Result { + pub fn custom_user_agent(mut self, custom_user_agent: &str) -> Result { self.set("custom_user_agent", custom_user_agent)?; Ok(self) } /// The order type used when none is specified ([ASC] or DESC) - pub fn default_order(mut self, order: DefaultOrder) -> Result { + pub fn default_order(mut self, order: DefaultOrder) -> Result { self.set("default_order", &order.to_string())?; Ok(self) } /// Null ordering used when none is specified ([NULLS_FIRST] or NULLS_LAST) - pub fn default_null_order(mut self, null_order: DefaultNullOrder) -> Result { + pub fn default_null_order(mut self, null_order: DefaultNullOrder) -> Result { self.set("default_null_order", &null_order.to_string())?; Ok(self) } /// Allow the database to access external state (through e.g. COPY TO/FROM, CSV readers, pandas replacement scans, etc) - pub fn enable_external_access(mut self, enabled: bool) -> Result { + pub fn enable_external_access(mut self, enabled: bool) -> Result { self.set("enable_external_access", &enabled.to_string())?; Ok(self) } /// Whether or not object cache is used to cache e.g. Parquet metadata - pub fn enable_object_cache(mut self, enabled: bool) -> Result { + pub fn enable_object_cache(mut self, enabled: bool) -> Result { self.set("enable_object_cache", &enabled.to_string())?; Ok(self) } /// Allow to load third-party duckdb extensions. - pub fn allow_unsigned_extensions(mut self) -> Result { + pub fn allow_unsigned_extensions(mut self) -> Result { self.set("allow_unsigned_extensions", "true")?; Ok(self) } /// The maximum memory of the system (e.g. 1GB) - pub fn max_memory(mut self, memory: &str) -> Result { + pub fn max_memory(mut self, memory: &str) -> Result { self.set("max_memory", memory)?; Ok(self) } /// The number of total threads used by the system - pub fn threads(mut self, thread_num: i64) -> Result { + pub fn threads(mut self, thread_num: i64) -> Result { self.set("threads", &thread_num.to_string())?; Ok(self) } /// Add any setting to the config. DuckDB will return an error if the setting is unknown or /// otherwise invalid. - pub fn with(mut self, key: impl AsRef, value: impl AsRef) -> Result { + pub fn with(mut self, key: impl AsRef, value: impl AsRef) -> Result { self.set(key.as_ref(), value.as_ref())?; Ok(self) } diff --git a/crates/duckdb/src/core/data_chunk.rs b/crates/duckdb/src/core/data_chunk.rs index 3ef35992..b464daa2 100644 --- a/crates/duckdb/src/core/data_chunk.rs +++ b/crates/duckdb/src/core/data_chunk.rs @@ -36,7 +36,7 @@ impl DataChunkHandle { let num_columns = logical_types.len(); let mut c_types = logical_types.iter().map(|t| t.ptr).collect::>(); let ptr = unsafe { duckdb_create_data_chunk(c_types.as_mut_ptr(), num_columns as u64) }; - DataChunkHandle { ptr, owned: true } + Self { ptr, owned: true } } /// Get the vector at the specific column index: `idx`. diff --git a/crates/duckdb/src/core/logical_type.rs b/crates/duckdb/src/core/logical_type.rs index ede3ba52..96faecd1 100644 --- a/crates/duckdb/src/core/logical_type.rs +++ b/crates/duckdb/src/core/logical_type.rs @@ -165,7 +165,7 @@ impl LogicalTypeHandle { } /// Creates a map type from its child type. - pub fn map(key: &LogicalTypeHandle, value: &LogicalTypeHandle) -> Self { + pub fn map(key: &Self, value: &Self) -> Self { unsafe { Self { ptr: duckdb_create_map_type(key.ptr, value.ptr), @@ -174,7 +174,7 @@ impl LogicalTypeHandle { } /// Creates a list type from its child type. - pub fn list(child_type: &LogicalTypeHandle) -> Self { + pub fn list(child_type: &Self) -> Self { unsafe { Self { ptr: duckdb_create_list_type(child_type.ptr), @@ -183,7 +183,7 @@ impl LogicalTypeHandle { } /// Creates an array type from its child type. - pub fn array(child_type: &LogicalTypeHandle, array_size: u64) -> Self { + pub fn array(child_type: &Self, array_size: u64) -> Self { unsafe { Self { ptr: duckdb_create_array_type(child_type.ptr, array_size), @@ -213,7 +213,7 @@ impl LogicalTypeHandle { } /// Make a `LogicalType` for `struct` - pub fn struct_type(fields: &[(&str, LogicalTypeHandle)]) -> Self { + pub fn struct_type(fields: &[(&str, Self)]) -> Self { let keys: Vec = fields.iter().map(|f| CString::new(f.0).unwrap()).collect(); let values: Vec = fields.iter().map(|it| it.1.ptr).collect(); let name_ptrs = keys.iter().map(|it| it.as_ptr()).collect::>(); @@ -230,7 +230,7 @@ impl LogicalTypeHandle { } /// Make a `LogicalType` for `union` - pub fn union_type(fields: &[(&str, LogicalTypeHandle)]) -> Self { + pub fn union_type(fields: &[(&str, Self)]) -> Self { let keys: Vec = fields.iter().map(|f| CString::new(f.0).unwrap()).collect(); let values: Vec = fields.iter().map(|it| it.1.ptr).collect(); let name_ptrs = keys.iter().map(|it| it.as_ptr()).collect::>(); diff --git a/crates/duckdb/src/core/vector.rs b/crates/duckdb/src/core/vector.rs index 5508e859..243d0b17 100644 --- a/crates/duckdb/src/core/vector.rs +++ b/crates/duckdb/src/core/vector.rs @@ -203,8 +203,8 @@ impl ListVector { } /// Take the child as [ListVector]. - pub fn list_child(&self) -> ListVector { - ListVector::from(unsafe { duckdb_list_vector_get_child(self.entries.ptr) }) + pub fn list_child(&self) -> Self { + Self::from(unsafe { duckdb_list_vector_get_child(self.entries.ptr) }) } /// Set primitive data to the child node. @@ -309,7 +309,7 @@ impl StructVector { } /// Take the child as [StructVector]. - pub fn struct_vector_child(&self, idx: usize) -> StructVector { + pub fn struct_vector_child(&self, idx: usize) -> Self { Self::from(unsafe { duckdb_struct_vector_get_child(self.ptr, idx as u64) }) } diff --git a/crates/duckdb/src/error.rs b/crates/duckdb/src/error.rs index b56e2c35..1a3763d8 100644 --- a/crates/duckdb/src/error.rs +++ b/crates/duckdb/src/error.rs @@ -86,23 +86,23 @@ pub enum Error { } impl PartialEq for Error { - fn eq(&self, other: &Error) -> bool { + fn eq(&self, other: &Self) -> bool { match (self, other) { - (Error::DuckDBFailure(e1, s1), Error::DuckDBFailure(e2, s2)) => e1 == e2 && s1 == s2, - (Error::IntegralValueOutOfRange(i1, n1), Error::IntegralValueOutOfRange(i2, n2)) => i1 == i2 && n1 == n2, - (Error::Utf8Error(e1), Error::Utf8Error(e2)) => e1 == e2, - (Error::NulError(e1), Error::NulError(e2)) => e1 == e2, - (Error::InvalidParameterName(n1), Error::InvalidParameterName(n2)) => n1 == n2, - (Error::InvalidPath(p1), Error::InvalidPath(p2)) => p1 == p2, - (Error::ExecuteReturnedResults, Error::ExecuteReturnedResults) => true, - (Error::QueryReturnedNoRows, Error::QueryReturnedNoRows) => true, - (Error::InvalidColumnIndex(i1), Error::InvalidColumnIndex(i2)) => i1 == i2, - (Error::InvalidColumnName(n1), Error::InvalidColumnName(n2)) => n1 == n2, - (Error::InvalidColumnType(i1, n1, t1), Error::InvalidColumnType(i2, n2, t2)) => { + (Self::DuckDBFailure(e1, s1), Self::DuckDBFailure(e2, s2)) => e1 == e2 && s1 == s2, + (Self::IntegralValueOutOfRange(i1, n1), Self::IntegralValueOutOfRange(i2, n2)) => i1 == i2 && n1 == n2, + (Self::Utf8Error(e1), Self::Utf8Error(e2)) => e1 == e2, + (Self::NulError(e1), Self::NulError(e2)) => e1 == e2, + (Self::InvalidParameterName(n1), Self::InvalidParameterName(n2)) => n1 == n2, + (Self::InvalidPath(p1), Self::InvalidPath(p2)) => p1 == p2, + (Self::ExecuteReturnedResults, Self::ExecuteReturnedResults) => true, + (Self::QueryReturnedNoRows, Self::QueryReturnedNoRows) => true, + (Self::InvalidColumnIndex(i1), Self::InvalidColumnIndex(i2)) => i1 == i2, + (Self::InvalidColumnName(n1), Self::InvalidColumnName(n2)) => n1 == n2, + (Self::InvalidColumnType(i1, n1, t1), Self::InvalidColumnType(i2, n2, t2)) => { i1 == i2 && t1 == t2 && n1 == n2 } - (Error::StatementChangedRows(n1), Error::StatementChangedRows(n2)) => n1 == n2, - (Error::InvalidParameterCount(i1, n1), Error::InvalidParameterCount(i2, n2)) => i1 == i2 && n1 == n2, + (Self::StatementChangedRows(n1), Self::StatementChangedRows(n2)) => n1 == n2, + (Self::InvalidParameterCount(i1, n1), Self::InvalidParameterCount(i2, n2)) => i1 == i2 && n1 == n2, (..) => false, } } @@ -110,15 +110,15 @@ impl PartialEq for Error { impl From for Error { #[cold] - fn from(err: str::Utf8Error) -> Error { - Error::Utf8Error(err) + fn from(err: str::Utf8Error) -> Self { + Self::Utf8Error(err) } } impl From<::std::ffi::NulError> for Error { #[cold] - fn from(err: ::std::ffi::NulError) -> Error { - Error::NulError(err) + fn from(err: ::std::ffi::NulError) -> Self { + Self::NulError(err) } } @@ -128,17 +128,17 @@ const UNKNOWN_COLUMN: usize = usize::MAX; /// to allow use of `get_raw(…).as_…()?` in callbacks that take `Error`. impl From for Error { #[cold] - fn from(err: FromSqlError) -> Error { + fn from(err: FromSqlError) -> Self { // The error type requires index and type fields, but they aren't known in this // context. match err { - FromSqlError::OutOfRange(val) => Error::IntegralValueOutOfRange(UNKNOWN_COLUMN, val), + FromSqlError::OutOfRange(val) => Self::IntegralValueOutOfRange(UNKNOWN_COLUMN, val), #[cfg(feature = "uuid")] FromSqlError::InvalidUuidSize(_) => { Error::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Blob, Box::new(err)) } - FromSqlError::Other(source) => Error::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, source), - _ => Error::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, Box::new(err)), + FromSqlError::Other(source) => Self::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, source), + _ => Self::FromSqlConversionFailure(UNKNOWN_COLUMN, Type::Null, Box::new(err)), } } } @@ -146,46 +146,46 @@ impl From for Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - Error::DuckDBFailure(ref err, None) => err.fmt(f), - Error::DuckDBFailure(_, Some(ref s)) => write!(f, "{s}"), - Error::FromSqlConversionFailure(i, ref t, ref err) => { + Self::DuckDBFailure(ref err, None) => err.fmt(f), + Self::DuckDBFailure(_, Some(ref s)) => write!(f, "{s}"), + Self::FromSqlConversionFailure(i, ref t, ref err) => { if i != UNKNOWN_COLUMN { write!(f, "Conversion error from type {t} at index: {i}, {err}") } else { err.fmt(f) } } - Error::IntegralValueOutOfRange(col, val) => { + Self::IntegralValueOutOfRange(col, val) => { if col != UNKNOWN_COLUMN { write!(f, "Integer {val} out of range at index {col}") } else { write!(f, "Integer {val} out of range") } } - Error::Utf8Error(ref err) => err.fmt(f), - Error::NulError(ref err) => err.fmt(f), - Error::InvalidParameterName(ref name) => write!(f, "Invalid parameter name: {name}"), - Error::InvalidPath(ref p) => write!(f, "Invalid path: {}", p.to_string_lossy()), - Error::ExecuteReturnedResults => { + Self::Utf8Error(ref err) => err.fmt(f), + Self::NulError(ref err) => err.fmt(f), + Self::InvalidParameterName(ref name) => write!(f, "Invalid parameter name: {name}"), + Self::InvalidPath(ref p) => write!(f, "Invalid path: {}", p.to_string_lossy()), + Self::ExecuteReturnedResults => { write!(f, "Execute returned results - did you mean to call query?") } - Error::QueryReturnedNoRows => write!(f, "Query returned no rows"), - Error::InvalidColumnIndex(i) => write!(f, "Invalid column index: {i}"), - Error::InvalidColumnName(ref name) => write!(f, "Invalid column name: {name}"), - Error::InvalidColumnType(i, ref name, ref t) => { + Self::QueryReturnedNoRows => write!(f, "Query returned no rows"), + Self::InvalidColumnIndex(i) => write!(f, "Invalid column index: {i}"), + Self::InvalidColumnName(ref name) => write!(f, "Invalid column name: {name}"), + Self::InvalidColumnType(i, ref name, ref t) => { write!(f, "Invalid column type {t} at index: {i}, name: {name}") } - Error::ArrowTypeToDuckdbType(ref name, ref t) => { + Self::ArrowTypeToDuckdbType(ref name, ref t) => { write!(f, "Invalid column type {t} , name: {name}") } - Error::InvalidParameterCount(i1, n1) => { + Self::InvalidParameterCount(i1, n1) => { write!(f, "Wrong number of parameters passed to query. Got {i1}, needed {n1}") } - Error::StatementChangedRows(i) => write!(f, "Query changed {i} rows"), - Error::ToSqlConversionFailure(ref err) => err.fmt(f), - Error::InvalidQuery => write!(f, "Query is not read-only"), - Error::MultipleStatement => write!(f, "Multiple statements provided"), - Error::AppendError => write!(f, "Append error"), + Self::StatementChangedRows(i) => write!(f, "Query changed {i} rows"), + Self::ToSqlConversionFailure(ref err) => err.fmt(f), + Self::InvalidQuery => write!(f, "Query is not read-only"), + Self::MultipleStatement => write!(f, "Multiple statements provided"), + Self::AppendError => write!(f, "Append error"), } } } @@ -193,25 +193,25 @@ impl fmt::Display for Error { impl error::Error for Error { fn source(&self) -> Option<&(dyn error::Error + 'static)> { match *self { - Error::DuckDBFailure(ref err, _) => Some(err), - Error::Utf8Error(ref err) => Some(err), - Error::NulError(ref err) => Some(err), - - Error::IntegralValueOutOfRange(..) - | Error::InvalidParameterName(_) - | Error::ExecuteReturnedResults - | Error::QueryReturnedNoRows - | Error::InvalidColumnIndex(_) - | Error::InvalidColumnName(_) - | Error::InvalidColumnType(..) - | Error::InvalidPath(_) - | Error::InvalidParameterCount(..) - | Error::StatementChangedRows(_) - | Error::InvalidQuery - | Error::AppendError - | Error::ArrowTypeToDuckdbType(..) - | Error::MultipleStatement => None, - Error::FromSqlConversionFailure(_, _, ref err) | Error::ToSqlConversionFailure(ref err) => Some(&**err), + Self::DuckDBFailure(ref err, _) => Some(err), + Self::Utf8Error(ref err) => Some(err), + Self::NulError(ref err) => Some(err), + + Self::IntegralValueOutOfRange(..) + | Self::InvalidParameterName(_) + | Self::ExecuteReturnedResults + | Self::QueryReturnedNoRows + | Self::InvalidColumnIndex(_) + | Self::InvalidColumnName(_) + | Self::InvalidColumnType(..) + | Self::InvalidPath(_) + | Self::InvalidParameterCount(..) + | Self::StatementChangedRows(_) + | Self::InvalidQuery + | Self::AppendError + | Self::ArrowTypeToDuckdbType(..) + | Self::MultipleStatement => None, + Self::FromSqlConversionFailure(_, _, ref err) | Self::ToSqlConversionFailure(ref err) => Some(&**err), } } } diff --git a/crates/duckdb/src/inner_connection.rs b/crates/duckdb/src/inner_connection.rs index f9d50c1c..5f377723 100644 --- a/crates/duckdb/src/inner_connection.rs +++ b/crates/duckdb/src/inner_connection.rs @@ -20,7 +20,7 @@ pub struct InnerConnection { impl InnerConnection { #[inline] - pub unsafe fn new(db: ffi::duckdb_database, owned: bool) -> Result { + pub unsafe fn new(db: ffi::duckdb_database, owned: bool) -> Result { let mut con: ffi::duckdb_connection = ptr::null_mut(); let r = ffi::duckdb_connect(db, &mut con); if r != ffi::DuckDBSuccess { @@ -30,10 +30,10 @@ impl InnerConnection { Some("connect error".to_owned()), )); } - Ok(InnerConnection { db, con, owned }) + Ok(Self { db, con, owned }) } - pub fn open_with_flags(c_path: &CStr, config: Config) -> Result { + pub fn open_with_flags(c_path: &CStr, config: Config) -> Result { unsafe { let mut db: ffi::duckdb_database = ptr::null_mut(); let mut c_err = std::ptr::null_mut(); @@ -43,7 +43,7 @@ impl InnerConnection { ffi::duckdb_free(c_err as *mut c_void); return Err(Error::DuckDBFailure(ffi::Error::new(r), msg)); } - InnerConnection::new(db, true) + Self::new(db, true) } } @@ -68,7 +68,7 @@ impl InnerConnection { /// Creates a new connection to the already-opened database. pub fn try_clone(&self) -> Result { - unsafe { InnerConnection::new(self.db, false) } + unsafe { Self::new(self.db, false) } } pub fn execute(&mut self, sql: &str) -> Result<()> { diff --git a/crates/duckdb/src/lib.rs b/crates/duckdb/src/lib.rs index b7d796bd..45bc2961 100644 --- a/crates/duckdb/src/lib.rs +++ b/crates/duckdb/src/lib.rs @@ -253,8 +253,8 @@ impl Connection { /// Will return `Err` if `path` cannot be converted to a C-compatible /// string or if the underlying DuckDB open call fails. #[inline] - pub fn open>(path: P) -> Result { - Connection::open_with_flags(path, Config::default()) + pub fn open>(path: P) -> Result { + Self::open_with_flags(path, Config::default()) } /// Open a new connection to an in-memory DuckDB database. @@ -263,8 +263,8 @@ impl Connection { /// /// Will return `Err` if the underlying DuckDB open call fails. #[inline] - pub fn open_in_memory() -> Result { - Connection::open_in_memory_with_flags(Config::default()) + pub fn open_in_memory() -> Result { + Self::open_in_memory_with_flags(Config::default()) } /// Open a new connection to an ffi database. @@ -276,8 +276,8 @@ impl Connection { /// /// Need to pass in a valid db instance #[inline] - pub unsafe fn open_from_raw(raw: ffi::duckdb_database) -> Result { - InnerConnection::new(raw, false).map(|db| Connection { + pub unsafe fn open_from_raw(raw: ffi::duckdb_database) -> Result { + InnerConnection::new(raw, false).map(|db| Self { db: RefCell::new(db), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), path: None, // Can we know the path from connection? @@ -291,7 +291,7 @@ impl Connection { /// Will return `Err` if `path` cannot be converted to a C-compatible /// string or if the underlying DuckDB open call fails. #[inline] - pub fn open_with_flags>(path: P, config: Config) -> Result { + pub fn open_with_flags>(path: P, config: Config) -> Result { #[cfg(unix)] fn path_to_cstring(p: &Path) -> Result { use std::os::unix::ffi::OsStrExt; @@ -306,7 +306,7 @@ impl Connection { let c_path = path_to_cstring(path.as_ref())?; let config = config.with("duckdb_api", "rust").unwrap(); - InnerConnection::open_with_flags(&c_path, config).map(|db| Connection { + InnerConnection::open_with_flags(&c_path, config).map(|db| Self { db: RefCell::new(db), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), path: Some(path.as_ref().to_path_buf()), @@ -319,8 +319,8 @@ impl Connection { /// /// Will return `Err` if the underlying DuckDB open call fails. #[inline] - pub fn open_in_memory_with_flags(config: Config) -> Result { - Connection::open_with_flags(":memory:", config) + pub fn open_in_memory_with_flags(config: Config) -> Result { + Self::open_with_flags(":memory:", config) } /// Convenience method to run multiple SQL statements (that cannot take any @@ -542,7 +542,7 @@ impl Connection { /// /// Will return `Err` if the underlying DuckDB call fails. #[inline] - pub fn close(self) -> Result<(), (Connection, Error)> { + pub fn close(self) -> Result<(), (Self, Error)> { let r = self.db.borrow_mut().close(); r.map_err(move |err| (self, err)) } @@ -557,7 +557,7 @@ impl Connection { /// Creates a new connection to the already-opened database. pub fn try_clone(&self) -> Result { let inner = self.db.borrow().try_clone()?; - Ok(Connection { + Ok(Self { db: RefCell::new(inner), cache: StatementCache::with_capacity(STATEMENT_CACHE_DEFAULT_CAPACITY), path: self.path.clone(), @@ -1025,8 +1025,8 @@ mod test { impl fmt::Display for CustomError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { match *self { - CustomError::SomeError => write!(f, "my custom error"), - CustomError::Sqlite(ref se) => write!(f, "my custom error: {se}"), + Self::SomeError => write!(f, "my custom error"), + Self::Sqlite(ref se) => write!(f, "my custom error: {se}"), } } } @@ -1038,15 +1038,15 @@ mod test { fn cause(&self) -> Option<&dyn StdError> { match *self { - CustomError::SomeError => None, - CustomError::Sqlite(ref se) => Some(se), + Self::SomeError => None, + Self::Sqlite(ref se) => Some(se), } } } impl From for CustomError { - fn from(se: Error) -> CustomError { - CustomError::Sqlite(se) + fn from(se: Error) -> Self { + Self::Sqlite(se) } } diff --git a/crates/duckdb/src/pragma.rs b/crates/duckdb/src/pragma.rs index 26fd387e..0b36dc49 100644 --- a/crates/duckdb/src/pragma.rs +++ b/crates/duckdb/src/pragma.rs @@ -14,8 +14,8 @@ pub struct Sql { } impl Sql { - pub fn new() -> Sql { - Sql { buf: String::new() } + pub fn new() -> Self { + Self { buf: String::new() } } pub fn push_pragma(&mut self, schema_name: Option>, pragma_name: &str) -> Result<()> { diff --git a/crates/duckdb/src/raw_statement.rs b/crates/duckdb/src/raw_statement.rs index ebe4a4b6..403f4f06 100644 --- a/crates/duckdb/src/raw_statement.rs +++ b/crates/duckdb/src/raw_statement.rs @@ -34,8 +34,8 @@ pub struct RawStatement { impl RawStatement { #[inline] - pub unsafe fn new(stmt: ffi::duckdb_prepared_statement) -> RawStatement { - RawStatement { + pub unsafe fn new(stmt: ffi::duckdb_prepared_statement) -> Self { + Self { ptr: stmt, result: None, schema: None, diff --git a/crates/duckdb/src/row.rs b/crates/duckdb/src/row.rs index 3a223032..eb6bbee7 100644 --- a/crates/duckdb/src/row.rs +++ b/crates/duckdb/src/row.rs @@ -104,7 +104,7 @@ impl<'stmt> Rows<'stmt> { impl<'stmt> Rows<'stmt> { #[inline] - pub(crate) fn new(stmt: &'stmt Statement<'stmt>) -> Rows<'stmt> { + pub(crate) fn new(stmt: &'stmt Statement<'stmt>) -> Self { Rows { stmt: Some(stmt), arr: Arc::new(None), diff --git a/crates/duckdb/src/types/from_sql.rs b/crates/duckdb/src/types/from_sql.rs index c0d37538..5ef2d441 100644 --- a/crates/duckdb/src/types/from_sql.rs +++ b/crates/duckdb/src/types/from_sql.rs @@ -25,10 +25,10 @@ pub enum FromSqlError { } impl PartialEq for FromSqlError { - fn eq(&self, other: &FromSqlError) -> bool { + fn eq(&self, other: &Self) -> bool { match (self, other) { - (FromSqlError::InvalidType, FromSqlError::InvalidType) => true, - (FromSqlError::OutOfRange(n1), FromSqlError::OutOfRange(n2)) => n1 == n2, + (Self::InvalidType, Self::InvalidType) => true, + (Self::OutOfRange(n1), Self::OutOfRange(n2)) => n1 == n2, #[cfg(feature = "uuid")] (FromSqlError::InvalidUuidSize(s1), FromSqlError::InvalidUuidSize(s2)) => s1 == s2, (..) => false, @@ -39,20 +39,20 @@ impl PartialEq for FromSqlError { impl fmt::Display for FromSqlError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - FromSqlError::InvalidType => write!(f, "Invalid type"), - FromSqlError::OutOfRange(i) => write!(f, "Value {i} out of range"), + Self::InvalidType => write!(f, "Invalid type"), + Self::OutOfRange(i) => write!(f, "Value {i} out of range"), #[cfg(feature = "uuid")] FromSqlError::InvalidUuidSize(s) => { write!(f, "Cannot read UUID value out of {s} byte blob") } - FromSqlError::Other(ref err) => err.fmt(f), + Self::Other(ref err) => err.fmt(f), } } } impl Error for FromSqlError { fn source(&self) -> Option<&(dyn Error + 'static)> { - if let FromSqlError::Other(ref err) = self { + if let Self::Other(ref err) = self { Some(&**err) } else { None diff --git a/crates/duckdb/src/types/mod.rs b/crates/duckdb/src/types/mod.rs index 4da528eb..87917b74 100644 --- a/crates/duckdb/src/types/mod.rs +++ b/crates/duckdb/src/types/mod.rs @@ -130,18 +130,18 @@ impl From<&DataType> for Type { // DataType::FixedSizeBinary(_) => Self::FixedSizeBinary, // DataType::LargeBinary => Self::LargeBinary, DataType::LargeUtf8 | DataType::Utf8 => Self::Text, - DataType::List(inner) => Self::List(Box::new(Type::from(inner.data_type()))), + DataType::List(inner) => Self::List(Box::new(Self::from(inner.data_type()))), DataType::FixedSizeList(field, size) => { - Self::Array(Box::new(Type::from(field.data_type())), (*size).try_into().unwrap()) + Self::Array(Box::new(Self::from(field.data_type())), (*size).try_into().unwrap()) } // DataType::LargeList(_) => Self::LargeList, DataType::Struct(inner) => Self::Struct( inner .iter() - .map(|f| (f.name().to_owned(), Type::from(f.data_type()))) + .map(|f| (f.name().to_owned(), Self::from(f.data_type()))) .collect(), ), - DataType::LargeList(inner) => Self::List(Box::new(Type::from(inner.data_type()))), + DataType::LargeList(inner) => Self::List(Box::new(Self::from(inner.data_type()))), // DataType::Union(_, _) => Self::Union, DataType::Decimal128(..) => Self::Decimal, DataType::Decimal256(..) => Self::Decimal, @@ -149,8 +149,8 @@ impl From<&DataType> for Type { let data_type = field.data_type(); match data_type { DataType::Struct(fields) => Self::Map( - Box::new(Type::from(fields[0].data_type())), - Box::new(Type::from(fields[1].data_type())), + Box::new(Self::from(fields[0].data_type())), + Box::new(Self::from(fields[1].data_type())), ), _ => unreachable!(), } @@ -163,33 +163,33 @@ impl From<&DataType> for Type { impl fmt::Display for Type { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - Type::Null => f.pad("Null"), - Type::Boolean => f.pad("Boolean"), - Type::TinyInt => f.pad("TinyInt"), - Type::SmallInt => f.pad("SmallInt"), - Type::Int => f.pad("Int"), - Type::BigInt => f.pad("BigInt"), - Type::HugeInt => f.pad("HugeInt"), - Type::UTinyInt => f.pad("UTinyInt"), - Type::USmallInt => f.pad("USmallInt"), - Type::UInt => f.pad("UInt"), - Type::UBigInt => f.pad("UBigInt"), - Type::Float => f.pad("Float"), - Type::Double => f.pad("Double"), - Type::Decimal => f.pad("Decimal"), - Type::Timestamp => f.pad("Timestamp"), - Type::Text => f.pad("Text"), - Type::Blob => f.pad("Blob"), - Type::Date32 => f.pad("Date32"), - Type::Time64 => f.pad("Time64"), - Type::Interval => f.pad("Interval"), - Type::Struct(..) => f.pad("Struct"), - Type::List(..) => f.pad("List"), - Type::Enum => f.pad("Enum"), - Type::Map(..) => f.pad("Map"), - Type::Array(..) => f.pad("Array"), - Type::Union => f.pad("Union"), - Type::Any => f.pad("Any"), + Self::Null => f.pad("Null"), + Self::Boolean => f.pad("Boolean"), + Self::TinyInt => f.pad("TinyInt"), + Self::SmallInt => f.pad("SmallInt"), + Self::Int => f.pad("Int"), + Self::BigInt => f.pad("BigInt"), + Self::HugeInt => f.pad("HugeInt"), + Self::UTinyInt => f.pad("UTinyInt"), + Self::USmallInt => f.pad("USmallInt"), + Self::UInt => f.pad("UInt"), + Self::UBigInt => f.pad("UBigInt"), + Self::Float => f.pad("Float"), + Self::Double => f.pad("Double"), + Self::Decimal => f.pad("Decimal"), + Self::Timestamp => f.pad("Timestamp"), + Self::Text => f.pad("Text"), + Self::Blob => f.pad("Blob"), + Self::Date32 => f.pad("Date32"), + Self::Time64 => f.pad("Time64"), + Self::Interval => f.pad("Interval"), + Self::Struct(..) => f.pad("Struct"), + Self::List(..) => f.pad("List"), + Self::Enum => f.pad("Enum"), + Self::Map(..) => f.pad("Map"), + Self::Array(..) => f.pad("Array"), + Self::Union => f.pad("Union"), + Self::Any => f.pad("Any"), } } } diff --git a/crates/duckdb/src/types/ordered_map.rs b/crates/duckdb/src/types/ordered_map.rs index 09d9392f..ac29ff0e 100644 --- a/crates/duckdb/src/types/ordered_map.rs +++ b/crates/duckdb/src/types/ordered_map.rs @@ -4,7 +4,7 @@ pub struct OrderedMap(Vec<(K, V)>); impl From> for OrderedMap { fn from(value: Vec<(K, V)>) -> Self { - OrderedMap(value) + Self(value) } } diff --git a/crates/duckdb/src/types/value.rs b/crates/duckdb/src/types/value.rs index 03c7b3dd..9cd7a127 100644 --- a/crates/duckdb/src/types/value.rs +++ b/crates/duckdb/src/types/value.rs @@ -71,29 +71,29 @@ pub enum Value { impl From for Value { #[inline] - fn from(_: Null) -> Value { - Value::Null + fn from(_: Null) -> Self { + Self::Null } } impl From for Value { #[inline] - fn from(i: bool) -> Value { - Value::Boolean(i) + fn from(i: bool) -> Self { + Self::Boolean(i) } } impl From for Value { #[inline] - fn from(i: usize) -> Value { - Value::UBigInt(i as u64) + fn from(i: usize) -> Self { + Self::UBigInt(i as u64) } } impl From for Value { #[inline] - fn from(i: isize) -> Value { - Value::BigInt(i as i64) + fn from(i: isize) -> Self { + Self::BigInt(i as i64) } } @@ -107,104 +107,104 @@ impl From for Value { impl From for Value { #[inline] - fn from(i: i8) -> Value { - Value::TinyInt(i) + fn from(i: i8) -> Self { + Self::TinyInt(i) } } impl From for Value { #[inline] - fn from(i: i16) -> Value { - Value::SmallInt(i) + fn from(i: i16) -> Self { + Self::SmallInt(i) } } impl From for Value { #[inline] - fn from(i: i32) -> Value { - Value::Int(i) + fn from(i: i32) -> Self { + Self::Int(i) } } impl From for Value { #[inline] - fn from(i: i64) -> Value { - Value::BigInt(i) + fn from(i: i64) -> Self { + Self::BigInt(i) } } impl From for Value { #[inline] - fn from(i: u8) -> Value { - Value::UTinyInt(i) + fn from(i: u8) -> Self { + Self::UTinyInt(i) } } impl From for Value { #[inline] - fn from(i: u16) -> Value { - Value::USmallInt(i) + fn from(i: u16) -> Self { + Self::USmallInt(i) } } impl From for Value { #[inline] - fn from(i: u32) -> Value { - Value::UInt(i) + fn from(i: u32) -> Self { + Self::UInt(i) } } impl From for Value { #[inline] - fn from(i: u64) -> Value { - Value::UBigInt(i) + fn from(i: u64) -> Self { + Self::UBigInt(i) } } impl From for Value { #[inline] - fn from(i: i128) -> Value { - Value::HugeInt(i) + fn from(i: i128) -> Self { + Self::HugeInt(i) } } impl From for Value { #[inline] - fn from(f: f32) -> Value { - Value::Float(f) + fn from(f: f32) -> Self { + Self::Float(f) } } impl From for Value { #[inline] - fn from(f: f64) -> Value { - Value::Double(f) + fn from(f: f64) -> Self { + Self::Double(f) } } impl From for Value { #[inline] - fn from(s: String) -> Value { - Value::Text(s) + fn from(s: String) -> Self { + Self::Text(s) } } impl From> for Value { #[inline] - fn from(v: Vec) -> Value { - Value::Blob(v) + fn from(v: Vec) -> Self { + Self::Blob(v) } } impl From> for Value where - T: Into, + T: Into, { #[inline] - fn from(v: Option) -> Value { + fn from(v: Option) -> Self { match v { Some(x) => x.into(), - None => Value::Null, + None => Self::Null, } } } @@ -214,28 +214,28 @@ impl Value { #[inline] pub fn data_type(&self) -> Type { match *self { - Value::Null => Type::Null, - Value::Boolean(_) => Type::Boolean, - Value::TinyInt(_) => Type::TinyInt, - Value::SmallInt(_) => Type::SmallInt, - Value::Int(_) => Type::Int, - Value::BigInt(_) => Type::BigInt, - Value::HugeInt(_) => Type::HugeInt, - Value::UTinyInt(_) => Type::UTinyInt, - Value::USmallInt(_) => Type::USmallInt, - Value::UInt(_) => Type::UInt, - Value::UBigInt(_) => Type::UBigInt, - Value::Float(_) => Type::Float, - Value::Double(_) => Type::Double, - Value::Decimal(_) => Type::Decimal, - Value::Timestamp(_, _) => Type::Timestamp, - Value::Text(_) => Type::Text, - Value::Blob(_) => Type::Blob, - Value::Date32(_) => Type::Date32, - Value::Time64(..) => Type::Time64, - Value::Interval { .. } => Type::Interval, - Value::Union(..) | Value::Struct(..) | Value::List(..) | Value::Array(..) | Value::Map(..) => todo!(), - Value::Enum(..) => Type::Enum, + Self::Null => Type::Null, + Self::Boolean(_) => Type::Boolean, + Self::TinyInt(_) => Type::TinyInt, + Self::SmallInt(_) => Type::SmallInt, + Self::Int(_) => Type::Int, + Self::BigInt(_) => Type::BigInt, + Self::HugeInt(_) => Type::HugeInt, + Self::UTinyInt(_) => Type::UTinyInt, + Self::USmallInt(_) => Type::USmallInt, + Self::UInt(_) => Type::UInt, + Self::UBigInt(_) => Type::UBigInt, + Self::Float(_) => Type::Float, + Self::Double(_) => Type::Double, + Self::Decimal(_) => Type::Decimal, + Self::Timestamp(_, _) => Type::Timestamp, + Self::Text(_) => Type::Text, + Self::Blob(_) => Type::Blob, + Self::Date32(_) => Type::Date32, + Self::Time64(..) => Type::Time64, + Self::Interval { .. } => Type::Interval, + Self::Union(..) | Self::Struct(..) | Self::List(..) | Self::Array(..) | Self::Map(..) => todo!(), + Self::Enum(..) => Type::Enum, } } } diff --git a/crates/duckdb/src/types/value_ref.rs b/crates/duckdb/src/types/value_ref.rs index 6a973865..db17b183 100644 --- a/crates/duckdb/src/types/value_ref.rs +++ b/crates/duckdb/src/types/value_ref.rs @@ -30,10 +30,10 @@ impl TimeUnit { /// Convert a number of `TimeUnit` to microseconds. pub fn to_micros(&self, value: i64) -> i64 { match self { - TimeUnit::Second => value * 1_000_000, - TimeUnit::Millisecond => value * 1000, - TimeUnit::Microsecond => value, - TimeUnit::Nanosecond => value / 1000, + Self::Second => value * 1_000_000, + Self::Millisecond => value * 1000, + Self::Microsecond => value, + Self::Nanosecond => value / 1000, } } } @@ -193,31 +193,31 @@ impl<'a> ValueRef<'a> { impl From> for Value { #[inline] - fn from(borrowed: ValueRef<'_>) -> Value { + fn from(borrowed: ValueRef<'_>) -> Self { match borrowed { - ValueRef::Null => Value::Null, - ValueRef::Boolean(i) => Value::Boolean(i), - ValueRef::TinyInt(i) => Value::TinyInt(i), - ValueRef::SmallInt(i) => Value::SmallInt(i), - ValueRef::Int(i) => Value::Int(i), - ValueRef::BigInt(i) => Value::BigInt(i), - ValueRef::HugeInt(i) => Value::HugeInt(i), - ValueRef::UTinyInt(i) => Value::UTinyInt(i), - ValueRef::USmallInt(i) => Value::USmallInt(i), - ValueRef::UInt(i) => Value::UInt(i), - ValueRef::UBigInt(i) => Value::UBigInt(i), - ValueRef::Float(i) => Value::Float(i), - ValueRef::Double(i) => Value::Double(i), - ValueRef::Decimal(i) => Value::Decimal(i), - ValueRef::Timestamp(tu, t) => Value::Timestamp(tu, t), + ValueRef::Null => Self::Null, + ValueRef::Boolean(i) => Self::Boolean(i), + ValueRef::TinyInt(i) => Self::TinyInt(i), + ValueRef::SmallInt(i) => Self::SmallInt(i), + ValueRef::Int(i) => Self::Int(i), + ValueRef::BigInt(i) => Self::BigInt(i), + ValueRef::HugeInt(i) => Self::HugeInt(i), + ValueRef::UTinyInt(i) => Self::UTinyInt(i), + ValueRef::USmallInt(i) => Self::USmallInt(i), + ValueRef::UInt(i) => Self::UInt(i), + ValueRef::UBigInt(i) => Self::UBigInt(i), + ValueRef::Float(i) => Self::Float(i), + ValueRef::Double(i) => Self::Double(i), + ValueRef::Decimal(i) => Self::Decimal(i), + ValueRef::Timestamp(tu, t) => Self::Timestamp(tu, t), ValueRef::Text(s) => { let s = std::str::from_utf8(s).expect("invalid UTF-8"); - Value::Text(s.to_string()) + Self::Text(s.to_string()) } - ValueRef::Blob(b) => Value::Blob(b.to_vec()), - ValueRef::Date32(d) => Value::Date32(d), - ValueRef::Time64(t, d) => Value::Time64(t, d), - ValueRef::Interval { months, days, nanos } => Value::Interval { months, days, nanos }, + ValueRef::Blob(b) => Self::Blob(b.to_vec()), + ValueRef::Date32(d) => Self::Date32(d), + ValueRef::Time64(t, d) => Self::Time64(t, d), + ValueRef::Interval { months, days, nanos } => Self::Interval { months, days, nanos }, ValueRef::List(items, idx) => match items { ListType::Regular(items) => { let offsets = items.offsets(); @@ -253,25 +253,25 @@ impl From> for Value { EnumType::UInt32(res) => res.key(idx), } .unwrap(); - Value::Enum(dict_values.value(dict_key).to_string()) + Self::Enum(dict_values.value(dict_key).to_string()) } ValueRef::Struct(items, idx) => { - let value: Vec<(String, Value)> = items + let value: Vec<(String, Self)> = items .columns() .iter() .zip(items.fields().iter().map(|f| f.name().to_owned())) - .map(|(column, name)| -> (String, Value) { + .map(|(column, name)| -> (String, Self) { (name, Row::value_ref_internal(idx, 0, column).to_owned()) }) .collect(); - Value::Struct(OrderedMap::from(value)) + Self::Struct(OrderedMap::from(value)) } ValueRef::Map(arr, idx) => { let keys = arr.keys(); let values = arr.values(); let offsets = arr.offsets(); let range = offsets[idx]..offsets[idx + 1]; - Value::Map(OrderedMap::from( + Self::Map(OrderedMap::from( range .map(|row| { let row = row.try_into().unwrap(); @@ -285,7 +285,7 @@ impl From> for Value { ValueRef::Array(items, idx) => { let value_length = usize::try_from(items.value_length()).unwrap(); let range = (idx * value_length)..((idx + 1) * value_length); - Value::Array( + Self::Array( range .map(|row| Row::value_ref_internal(row, idx, items.values()).to_owned()) .collect(), @@ -297,7 +297,7 @@ impl From> for Value { let value_offset = column.value_offset(idx); let tag = Row::value_ref_internal(idx, value_offset, column.child(type_id)); - Value::Union(Box::new(tag.to_owned())) + Self::Union(Box::new(tag.to_owned())) } } } @@ -327,7 +327,7 @@ impl<'a> From<&'a [u8]> for ValueRef<'a> { impl<'a> From<&'a Value> for ValueRef<'a> { #[inline] - fn from(value: &'a Value) -> ValueRef<'a> { + fn from(value: &'a Value) -> Self { match *value { Value::Null => ValueRef::Null, Value::Boolean(i) => ValueRef::Boolean(i), @@ -359,10 +359,10 @@ impl<'a> From<&'a Value> for ValueRef<'a> { impl<'a, T> From> for ValueRef<'a> where - T: Into>, + T: Into, { #[inline] - fn from(s: Option) -> ValueRef<'a> { + fn from(s: Option) -> Self { match s { Some(x) => x.into(), None => ValueRef::Null,