Repository: launchbadge/sqlx
File: sqlx-core/src/arguments.rs (Rust)

Code before:
//! Types and traits for passing arguments to SQL queries.

use crate::database::{Database, HasArguments};
use crate::encode::Encode;
use crate::types::Type;

/// A tuple of arguments to be sent to the database.
pub trait Arguments<'q>: Send + Sized + Default {
    type Database: Database;

    /// Reserves the capacity for at least `additional` more values (of `size` total bytes) to
    /// be added to the arguments without a reallocation.
    fn reserve(&mut self, additional: usize, size: usize);

    /// Add the value to the end of the arguments.
    fn add<T>(&mut self, value: T)
    where
        T: 'q + Send + Encode<'q, Self::Database> + Type<Self::Database>;

    fn place_holder(&self, _argument_count: u16) -> String {
        "?".to_string()
    }
}

pub trait IntoArguments<'q, DB: HasArguments<'q>>: Sized + Send {
    fn into_arguments(self) -> <DB as HasArguments<'q>>::Arguments;
}

// NOTE: required due to lack of lazy normalization
#[allow(unused_macros)]
macro_rules! impl_into_arguments_for_arguments {
    ($Arguments:path) => {
        impl<'q>
            crate::arguments::IntoArguments<
                'q,
                <$Arguments as crate::arguments::Arguments<'q>>::Database,
            > for $Arguments
        {
            fn into_arguments(self) -> $Arguments {
                self
            }
        }
    };
}


Review comment (suggestion):
Nit: `argument_count` shouldn't be necessary as the `Arguments` impl should already know how many arguments have been pushed. Personally, I also think "placeholder" should be one word, not two.

The intermediate `String` is also a wasted allocation. An alternative signature might look like this:
```rust
use std::fmt::{Self, Write};

fn format_placeholder<W: Write>(&self, mut writer: W) -> fmt::Result {
    writer.write_str("?")
}
```

and the Postgres implementation would look like this:

```rust
fn format_placeholder<W: Write>(&self, mut writer: W) -> fmt::Result {
    write!(writer, "${}", self.count)
}
```

`QueryBuilder` would then simply invoke it like:
```rust
// This is highly unlikely to return an error so panicking is fine.
arguments.format_placeholder(&mut self.query).expect("error in format_placeholder")
```

Code after:
//! Types and traits for passing arguments to SQL queries.

use crate::database::{Database, HasArguments};
use crate::encode::Encode;
use crate::types::Type;
use std::fmt::{self, Write};

/// A tuple of arguments to be sent to the database.
pub trait Arguments<'q>: Send + Sized + Default {
    type Database: Database;

    /// Reserves the capacity for at least `additional` more values (of `size` total bytes) to
    /// be added to the arguments without a reallocation.
    fn reserve(&mut self, additional: usize, size: usize);

    /// Add the value to the end of the arguments.
    fn add<T>(&mut self, value: T)
    where
        T: 'q + Send + Encode<'q, Self::Database> + Type<Self::Database>;

    fn format_placeholder<W: Write>(&self, writer: &mut W) -> fmt::Result {
        writer.write_str("?")
    }
}

pub trait IntoArguments<'q, DB: HasArguments<'q>>: Sized + Send {
    fn into_arguments(self) -> <DB as HasArguments<'q>>::Arguments;
}

// NOTE: required due to lack of lazy normalization
#[allow(unused_macros)]
macro_rules! impl_into_arguments_for_arguments {
    ($Arguments:path) => {
        impl<'q>
            crate::arguments::IntoArguments<
                'q,
                <$Arguments as crate::arguments::Arguments<'q>>::Database,
            > for $Arguments
        {
            fn into_arguments(self) -> $Arguments {
                self
            }
        }
    };
}