From d70286e98aca401be5332a7f481fb413658b4417 Mon Sep 17 00:00:00 2001 From: gwenn Date: Sat, 9 Feb 2019 07:16:05 +0100 Subject: [PATCH] Remove unwrap from examples --- README.md | 16 ++++++++-------- src/blob.rs | 27 ++++++++++++--------------- src/functions.rs | 10 +++++----- src/lib.rs | 19 ++++++++----------- 4 files changed, 33 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index f2d983e..46912b2 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ an interface similar to [rust-postgres](https://github.com/sfackler/rust-postgre ```rust use rusqlite::types::ToSql; -use rusqlite::{Connection, NO_PARAMS}; +use rusqlite::{Connection, Result, NO_PARAMS}; use time::Timespec; #[derive(Debug)] @@ -22,8 +22,8 @@ struct Person { data: Option>, } -fn main() { - let conn = Connection::open_in_memory().unwrap(); +fn main() -> Result<()> { + let conn = Connection::open_in_memory()?; conn.execute( "CREATE TABLE person ( @@ -33,7 +33,7 @@ fn main() { data BLOB )", NO_PARAMS, - ).unwrap(); + )?; let me = Person { id: 0, name: "Steven".to_string(), @@ -44,22 +44,22 @@ fn main() { "INSERT INTO person (name, time_created, data) VALUES (?1, ?2, ?3)", &[&me.name as &ToSql, &me.time_created, &me.data], - ).unwrap(); + )?; let mut stmt = conn - .prepare("SELECT id, name, time_created, data FROM person") - .unwrap(); + .prepare("SELECT id, name, time_created, data FROM person")?; let person_iter = stmt .query_map(NO_PARAMS, |row| Person { id: row.get(0), name: row.get(1), time_created: row.get(2), data: row.get(3), - }).unwrap(); + })?; for person in person_iter { println!("Found person {:?}", person.unwrap()); } + Ok(()) } ``` diff --git a/src/blob.rs b/src/blob.rs index 0f3d168..36d5b50 100644 --- a/src/blob.rs +++ b/src/blob.rs @@ -15,44 +15,41 @@ //! //! ```rust //! use rusqlite::blob::ZeroBlob; -//! use rusqlite::{Connection, DatabaseName, NO_PARAMS}; +//! use rusqlite::{Connection, DatabaseName, Result, NO_PARAMS}; //! use std::io::{Read, Seek, SeekFrom, Write}; //! -//! fn main() { -//! let db = Connection::open_in_memory().unwrap(); -//! db.execute_batch("CREATE TABLE test (content BLOB);") -//! .unwrap(); +//! fn main() -> Result<()> { +//! let db = Connection::open_in_memory()?; +//! db.execute_batch("CREATE TABLE test (content BLOB);")?; //! db.execute( //! "INSERT INTO test (content) VALUES (ZEROBLOB(10))", //! NO_PARAMS, -//! ) -//! .unwrap(); +//! )?; //! //! let rowid = db.last_insert_rowid(); //! let mut blob = db -//! .blob_open(DatabaseName::Main, "test", "content", rowid, false) -//! .unwrap(); +//! .blob_open(DatabaseName::Main, "test", "content", rowid, false)?; //! //! // Make sure to test that the number of bytes written matches what you expect; //! // if you try to write too much, the data will be truncated to the size of the //! // BLOB. -//! let bytes_written = blob.write(b"01234567").unwrap(); +//! let bytes_written = blob.write(b"01234567")?; //! assert_eq!(bytes_written, 8); //! //! // Same guidance - make sure you check the number of bytes read! -//! blob.seek(SeekFrom::Start(0)).unwrap(); +//! blob.seek(SeekFrom::Start(0))?; //! let mut buf = [0u8; 20]; -//! let bytes_read = blob.read(&mut buf[..]).unwrap(); +//! let bytes_read = blob.read(&mut buf[..])?; //! assert_eq!(bytes_read, 10); // note we read 10 bytes because the blob has size 10 //! -//! db.execute("INSERT INTO test (content) VALUES (?)", &[ZeroBlob(64)]) -//! .unwrap(); +//! db.execute("INSERT INTO test (content) VALUES (?)", &[ZeroBlob(64)])?; //! //! // given a new row ID, we can reopen the blob on that row //! let rowid = db.last_insert_rowid(); -//! blob.reopen(rowid).unwrap(); +//! blob.reopen(rowid)?; //! //! assert_eq!(blob.size(), 64); +//! Ok(()) //! } //! ``` use std::cmp::min; diff --git a/src/functions.rs b/src/functions.rs index ddacb84..c3ea25c 100644 --- a/src/functions.rs +++ b/src/functions.rs @@ -34,19 +34,19 @@ //! }) //! } //! -//! fn main() { -//! let db = Connection::open_in_memory().unwrap(); -//! add_regexp_function(&db).unwrap(); +//! fn main() -> Result<()> { +//! let db = Connection::open_in_memory()?; +//! add_regexp_function(&db)?; //! //! let is_match: bool = db //! .query_row( //! "SELECT regexp('[aeiou]*', 'aaaaeeeiii')", //! NO_PARAMS, //! |row| row.get(0), -//! ) -//! .unwrap(); +//! )?; //! //! assert!(is_match); +//! Ok(()) //! } //! ``` use std::error::Error as StdError; diff --git a/src/lib.rs b/src/lib.rs index a2dc60e..eddb794 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! //! ```rust //! use rusqlite::types::ToSql; -//! use rusqlite::{params, Connection}; +//! use rusqlite::{params, Connection, Result}; //! use time::Timespec; //! //! #[derive(Debug)] @@ -14,8 +14,8 @@ //! data: Option>, //! } //! -//! fn main() { -//! let conn = Connection::open_in_memory().unwrap(); +//! fn main() -> Result<()> { +//! let conn = Connection::open_in_memory()?; //! //! conn.execute( //! "CREATE TABLE person ( @@ -25,8 +25,7 @@ //! data BLOB //! )", //! params![], -//! ) -//! .unwrap(); +//! )?; //! let me = Person { //! id: 0, //! name: "Steven".to_string(), @@ -37,24 +36,22 @@ //! "INSERT INTO person (name, time_created, data) //! VALUES (?1, ?2, ?3)", //! params![me.name, me.time_created, me.data], -//! ) -//! .unwrap(); +//! )?; //! //! let mut stmt = conn -//! .prepare("SELECT id, name, time_created, data FROM person") -//! .unwrap(); +//! .prepare("SELECT id, name, time_created, data FROM person")?; //! let person_iter = stmt //! .query_map(params![], |row| Person { //! id: row.get(0), //! name: row.get(1), //! time_created: row.get(2), //! data: row.get(3), -//! }) -//! .unwrap(); +//! })?; //! //! for person in person_iter { //! println!("Found person {:?}", person.unwrap()); //! } +//! Ok(()) //! } //! ``` #![allow(unknown_lints)]