Skip to content

Re-design TryGetable to unify optional value decoding #3201

Description

@Huliiiiii

Currently, we first try to decode the value normally. If decoding fails (because it's null), an error is created. For Option<T>, we then drop that error and return None instead. This means decoding an optional value still constructs an error string on the failure path, which adds unnecessary allocation and overhead.

Ideally, we should provide a blanket implementation for Option<T>. Then users would only need to implement the conversion from our value for their own types.

This is a breaking change because it requires users to rewrite all their implementation to add the new method.

Example:

trait TryGetable {
    fn try_get_by<I: ColIdx>(
        row: &QueryResult,
        index: I,
    ) -> Result<Self, TryGetError> {
        let value = row.value(index)?;
        Self::try_from_value(value)
    }
}

impl<T> TryGetable for Option<T> {
    fn try_from_value(value: &Value) {
        if value.is_null() {
            return Ok(None);
        }
        
        T::try_from_value(value).map(Some)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions