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

Code before:

            #[cfg(feature = "sqlite")]
            AnyRowKind::Sqlite(row) => row.try_get_raw(index).map(Into::into),

            #[cfg(feature = "mssql")]
            AnyRowKind::Mssql(row) => row.try_get_raw(index).map(Into::into),
        }
    }

    fn try_get<'r, T, I>(&'r self, index: I) -> Result<T, Error>
    where
        I: ColumnIndex<Self>,
        T: Decode<'r, Self::Database> + Type<Self::Database>,
    {
        let value = self.try_get_raw(&index)?;

        if !value.is_null() {
            let ty = value.type_info();

            if !ty.is_null() && !T::compatible(&ty) {
                return Err(Error::ColumnDecode {
                    index: format!("{:?}", index),
                    source: mismatched_types::<T>(&ty),
                });
            }
        }

        T::decode(value).map_err(|source| Error::ColumnDecode {
            index: format!("{:?}", index),
            source,
        })
    }
}

impl<'i> ColumnIndex<AnyRow> for &'i str
where
    &'i str: AnyColumnIndex,
{
    fn index(&self, row: &AnyRow) -> Result<usize, Error> {
        match &row.kind {
            #[cfg(feature = "postgres")]
            AnyRowKind::Postgres(row) => self.index(row),

            #[cfg(feature = "mysql")]
            AnyRowKind::MySql(row) => self.index(row),

            #[cfg(feature = "sqlite")]
            AnyRowKind::Sqlite(row) => self.index(row),

            #[cfg(feature = "mssql")]
            AnyRowKind::Mssql(row) => self.index(row),

Review comment (performance):
Does this current implementation optimize out `.type_info()` access? Or, can we simply do this instead?

```suggestion
        let value = self.try_get_raw(&index)?;
        let ty = value.type_info();

        if !value.is_null() && !ty.is_null() && !T::compatible(&ty) {
            Err(mismatched_types::<T>(&ty))
        } else {
            T::decode(value)
        }
        .map_err(|source| Error::ColumnDecode {
            index: format!("{:?}", index),
            source,
        })
```

Code after:

            #[cfg(feature = "sqlite")]
            AnyRowKind::Sqlite(row) => row.try_get_raw(index).map(Into::into),

            #[cfg(feature = "mssql")]
            AnyRowKind::Mssql(row) => row.try_get_raw(index).map(Into::into),
        }
    }

    fn try_get<'r, T, I>(&'r self, index: I) -> Result<T, Error>
    where
        I: ColumnIndex<Self>,
        T: Decode<'r, Self::Database> + Type<Self::Database>,
    {
        let value = self.try_get_raw(&index)?;
        let ty = value.type_info();

        if !value.is_null() && !ty.is_null() && !T::compatible(&ty) {
            Err(mismatched_types::<T>(&ty))
        } else {
            T::decode(value)
        }
        .map_err(|source| Error::ColumnDecode {
            index: format!("{:?}", index),
            source,
        })
    }
}

impl<'i> ColumnIndex<AnyRow> for &'i str
where
    &'i str: AnyColumnIndex,
{
    fn index(&self, row: &AnyRow) -> Result<usize, Error> {
        match &row.kind {
            #[cfg(feature = "postgres")]
            AnyRowKind::Postgres(row) => self.index(row),

            #[cfg(feature = "mysql")]
            AnyRowKind::MySql(row) => self.index(row),

            #[cfg(feature = "sqlite")]
            AnyRowKind::Sqlite(row) => self.index(row),

            #[cfg(feature = "mssql")]
            AnyRowKind::Mssql(row) => self.index(row),
        }
    }
}