Return

Enum Return 

Source
pub enum Return<T, E = ()> {
    Success(T),
    Info(T),
    Error(E),
}
Expand description

Holds result and indicates the overall success or failure of a function.

Variants§

§

Success(T)

The function has been executed successfully. Holds result.

§

Info(T)

The function has been executed successfully. There have been warnings. Holds result.

§

Error(E)

An error occured.

Implementations§

Source§

impl<T, E> Return<T, E>

Source

pub fn map<F, U>(self, f: F) -> Return<U, E>
where F: FnOnce(T) -> U,

Maps a Return<T,E> to Return<U,E> by applying a function to a contained Success or Info value, leaving an Error value untouched.

Source

pub fn map_error<F, U>(self, f: F) -> Return<T, U>
where F: FnOnce(E) -> U,

Maps a Return<T,E> to Result<T,U> by applying a function to a contained Error value, leaving a Successor anInfo` value untouched.

Source

pub fn unwrap(self) -> T

Unwraps the result, yielding the content of Success or Info

Examples found in repository?
examples/bind_columns.rs (line 47)
45fn main() {
46
47    let env = Environment::new().unwrap();
48    let env = env.declare_version_3().unwrap();
49
50    match run(&env) {
51        Ok(()) => (),
52        Err(LastError(message)) => println!("An error occurred: {}", message),
53    }
54}
55
56fn run(env: &Environment<Odbc3>) -> MyResult<()> {
57
58    let conn = connect(env)?;
59    let result_set = execute_query(&conn)?;
60    print_fields(result_set)
61}
62
63fn connect<V>(env: &Environment<V>) -> MyResult<Connection<impl AutocommitMode>>
64where
65    V: Version,
66{
67    let conn = DataSource::with_parent(env).unwrap();
68    conn.connect("TestDataSource", "", "").into_result()
69}
70
71fn execute_query<'a, AC: AutocommitMode>(conn: &'a Connection<AC>) -> MyResult<ResultSet<'a, 'a, 'a, Unprepared>> {
72    let stmt = Statement::with_parent(conn).unwrap();
73    match stmt.exec_direct("SELECT year, title FROM Movies") {
74        ReturnOption::Success(s) |
75        ReturnOption::Info(s) => Ok(s),
76        ReturnOption::NoData(_) => Err(LastError(
77            "Statement did not return a Result Set.".to_owned(),
78        )),
79        ReturnOption::Error(e) => Err(e.into()),
80    }
81}
More examples
Hide additional examples
examples/print_table.rs (line 47)
45fn main() {
46
47    let env = Environment::new().unwrap();
48    let env = env.declare_version_3().unwrap();
49
50    match run(&env) {
51        Ok(()) => (),
52        Err(LastError(message)) => println!("An error occurred: {}", message),
53    }
54}
55
56fn run(env: &Environment<Odbc3>) -> MyResult<()> {
57
58    let conn = connect(&env)?;
59    let result_set = execute_query(&conn)?;
60    print_fields(result_set)
61}
62
63fn connect<V>(env: &Environment<V>) -> MyResult<Connection<impl AutocommitMode>>
64where
65    V: Version,
66{
67    let conn = DataSource::with_parent(env).unwrap();
68    conn.connect("TestDataSource", "", "").into_result()
69}
70
71fn execute_query<'a, AC: AutocommitMode>(conn: &'a Connection<AC>) -> MyResult<ResultSet<'a, 'a, 'a, Unprepared>> {
72    let stmt = Statement::with_parent(conn).unwrap();
73    match stmt.exec_direct("SELECT * FROM MOVIES") {
74        ReturnOption::Success(s) |
75        ReturnOption::Info(s) => Ok(s),
76        ReturnOption::NoData(_) => Err(LastError(
77            "Statement did not return a Result Set.".to_owned(),
78        )),
79        ReturnOption::Error(e) => Err(e.into()),
80    }
81}
82
83fn print_fields(result_set: ResultSet<Unprepared>) -> MyResult<()> {
84    let cols = result_set.num_result_cols().unwrap();
85    let mut buffer = [0u8; 512];
86    let mut cursor = match result_set.fetch() {
87        ReturnOption::Success(r) |
88        ReturnOption::Info(r) => r,
89        ReturnOption::NoData(_) => return Ok(()),
90        ReturnOption::Error(e) => return Err(e.into()),
91    };
92    loop {
93        for index in 1..(cols + 1) {
94            match cursor.get_data(index as u16, &mut buffer as &mut [u8]) {
95                ReturnOption::Success(ind) |
96                ReturnOption::Info(ind) => {
97                    match ind {
98                        Indicator::NoTotal => panic!("No Total"),
99                        Indicator::Null => println!("NULL"),
100                        Indicator::Length(l) => {
101                            print!("{}", from_utf8(&buffer[0..l as usize]).unwrap());
102                        }
103                    }
104                }
105                ReturnOption::NoData(_) => panic!("No Field Data"),
106                ReturnOption::Error(_) => return Err(cursor.into()),
107            }
108            print!(" | ");
109        }
110        cursor = match cursor.fetch() {
111            ReturnOption::Success(r) |
112            ReturnOption::Info(r) => r,
113            ReturnOption::NoData(_) => break Ok(()),
114            ReturnOption::Error(e) => break Err(e.into()),
115        };
116        println!("");
117    }
118}
examples/prepared_query.rs (line 8)
6fn main() {
7
8    let env = Environment::new().unwrap();
9    let env = env.declare_version_3().unwrap();
10    let conn = connect(&env);
11    let mut stmt = prepare_query(&conn);
12    for &year in [1968, 1993].iter() {
13        let result_set = execute_query(stmt, year);
14        stmt = print_fields(result_set);
15        println!("");
16    }
17}
18
19fn connect<V>(env: &Environment<V>) -> Connection<impl AutocommitMode>
20where
21    V: Version,
22{
23    let conn = DataSource::with_parent(env).unwrap();
24    conn.connect("TestDataSource", "", "").unwrap()
25}
26
27fn prepare_query<'a, AC: AutocommitMode>(conn: &'a Connection<AC>) -> Statement<'a, 'a, 'a, NoCursor, Prepared> {
28    let stmt = Statement::with_parent(conn).unwrap();
29    stmt.prepare("SELECT TITLE FROM MOVIES WHERE YEAR = ?")
30        .unwrap()
31}
32
33fn execute_query<'a>(
34    stmt: Statement<'a, 'a, 'a, NoCursor, Prepared>,
35    year: i32,
36) -> ResultSet<'a, 'a, 'a, Prepared> {
37    let stmt = stmt.bind_input_parameter(1, DataType::Integer, &year, None)
38        .unwrap();
39    let stmt = match stmt.execute() {
40        ReturnOption::Success(s) |
41        ReturnOption::Info(s) => s,
42        ReturnOption::NoData(_) |
43        ReturnOption::Error(_) => panic!("No Result Set"),
44    };
45    stmt.reset_parameters()
46}
examples/affected_row_count.rs (line 7)
6fn main() {
7    let env = Environment::new().unwrap();
8    let env = env.declare_version_3().unwrap();
9    let conn = DataSource::with_parent(&env).unwrap();
10    let conn = conn.connect("TestDataSource", "", "").unwrap();
11    exec(&conn, "INSERT INTO movies (title, year) VALUES ('TEST movie', 9999), ('TEST movie', 9998)");
12    exec(&conn, "DELETE FROM movies WHERE title = 'TEST movie'");
13}
14
15fn exec(conn: &Connection<AutocommitOn>, sql: &str) {
16    let stmt = Statement::with_parent(conn).unwrap();
17    let rs = match stmt.exec_direct(sql) {
18        ReturnOption::Success(s) |
19        ReturnOption::Info(s) => Ok(s),
20        ReturnOption::NoData(_) => Err("Statement did not return a Result Set.".to_owned()),
21        ReturnOption::Error(_) => Err("Error".to_owned()),
22    };
23
24    let row_count = rs.unwrap().affected_row_count();
25    println!("Affected row count for last statement: {:?}", row_count);
26}
examples/transactions.rs (line 6)
5fn main() {
6    let env = Environment::new().unwrap();
7    let env = env.declare_version_3().unwrap();
8
9    let ds = DataSource::with_parent(&env).unwrap();
10    let conn = ds.connect("TestDataSource", "", "").unwrap();
11    let mut conn = conn.disable_autocommit().unwrap();
12
13    {
14        //Any statement now will start transaction which could be ended with conn.commit() or conn.rollback()
15        //If either commit or rollback was not called before connection drop automatic rollback will be issued
16        let stmt = Statement::with_parent(&conn).unwrap();
17        let res = stmt.exec_direct("SELECT 'HELLO' FROM MOVIES");
18        println!("Result {:?}", res);
19    }
20
21    let end_tx_result = conn.commit();
22
23    println!("End TX result {:?}", end_tx_result);
24}
examples/list_datasources.rs (line 10)
8fn main() {
9
10    let env = Environment::new().unwrap();
11    let mut env = env.declare_version_3().unwrap();
12
13    let mut server_name = [0; 512];
14    let mut description = [0; 512];
15
16    println!("ODBC Data Sources:");
17
18    loop {
19        let (name_length, description_length) =
20            match env.data_sources(SQL_FETCH_NEXT, &mut server_name, &mut description) {
21                ReturnOption::Success(v) => v,
22                ReturnOption::Info(_) => panic!("Buffers not large enough. Truncation occurred."),
23                ReturnOption::NoData(()) => break,
24                ReturnOption::Error(()) => {
25                    panic!("Error occurred. Could use diagnostics to learn more")
26                }
27            };
28
29        println!(
30            "\tName: {}\n\tDescription: {}\n",
31            from_utf8(&server_name[..(name_length as usize)]).unwrap(),
32            from_utf8(&description[..(description_length as usize)]).unwrap()
33        );
34    }
35}
Source

pub fn success<U>(self) -> Result<T, U>
where U: From<E>,

Transforms the Return<T,E> into a Result<T,U>, mapping Success(v) | Info(v) to Ok(v) and Error(err) to Err(err.into()).

Trait Implementations§

Source§

impl<T: Debug, E: Debug> Debug for Return<T, E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S, E> Diagnostics for Return<S, E>
where S: Diagnostics, E: Diagnostics,

Source§

fn diagnostics( &self, rec_number: SQLSMALLINT, message_text: &mut [SQLCHAR], ) -> ReturnOption<DiagResult>

Returns the current values of multiple fields of a diagnostic record that contains error, warning, and status information. Read more
Source§

impl From<SQLRETURN> for Return<()>

Source§

fn from(source: SQLRETURN) -> Return<()>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T, E> Freeze for Return<T, E>
where T: Freeze, E: Freeze,

§

impl<T, E> RefUnwindSafe for Return<T, E>

§

impl<T, E> Send for Return<T, E>
where T: Send, E: Send,

§

impl<T, E> Sync for Return<T, E>
where T: Sync, E: Sync,

§

impl<T, E> Unpin for Return<T, E>
where T: Unpin, E: Unpin,

§

impl<T, E> UnwindSafe for Return<T, E>
where T: UnwindSafe, E: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.