-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API improvements for usage of owned Element
s
#858
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #858 +/- ##
==========================================
+ Coverage 77.55% 77.81% +0.26%
==========================================
Files 135 136 +1
Lines 33796 33984 +188
Branches 33796 33984 +188
==========================================
+ Hits 26210 26446 +236
+ Misses 5645 5598 -47
+ Partials 1941 1940 -1 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good, thanks! One question about our error types before we merge this.
src/result/conversion.rs
Outdated
use crate::{Bytes, Decimal, Int, IonType, Sequence, Str, Struct, Symbol, Timestamp}; | ||
use thiserror::Error; | ||
|
||
pub type ConversionResult<T> = Result<T, ConversionError>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I was expecting Conversion
and ConversionResult
to be the same type, something like:
pub type ConversionResult<FromType, ToType> = Result<ToType, ConversionError<FromType>>;
impl <FromType, ToType> From<ConversionResult<FromType, ToType>> for IonResult<ToType> {
fn from(conversion: ConversionResult<FromType, ToType>) -> Self {
match conversion {
Ok(value) => Ok(value),
Err(conversion_error) => {
Err(IonError::Conversion(conversion_err.message())) // some text explanation
}
}
}
}
This way the try_into_*
methods returning a ConversionResult
would benefit from all of the Result
methods (including ok()
for Option
conversion). You could then also use ?
with ConvesionResult
in contexts that needed to return an IonResult
, which would be nice.
Could you explain the motivation for distinct Conversion
/ConversionResult
types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ConversionError<FromType>
can't be put directly into IonError
due to the generic.
pub enum IonError {
...
#[error("{0}")]
Conversion(#[from] ConversionError<FromType>),
...
}
So you end up needing two 'error' structs anyway.
I originally had something like:
/// Represents a mismatch during conversion between the expected type and the actual value's type.
#[derive(Clone, Debug, Error, PartialEq)]
#[error("Type conversion error; expected a(n) {output_type}, found a(n) {input_type}")]
pub struct ConversionError {
input_value: String,
output_type: String,
}
/// Represents a mismatch during conversion between the expected type and the actual value's type.
#[derive(Clone, Debug, Error, PartialEq)]
#[error("Type conversion error; expected a(n) {output_type}, found a(n) {input_type}")]
pub struct ConversionOperationError<FromType>
where
FromType: ValueTypeExpectation,
{
input_value: FromType,
output_type: String,
}
So, a pair of Error
s and a pair of Result
s. Except the ConversionOperationError
/ConversionOperationResult
basically never existed for long and wasn't really used as an error; It was either unwrapped for its success or fail element or converted immediately into a ConversionError
/ConversionResult
. On top of this, I kept confusing myself as to which was which between Conversion<x>
and ConversionOperation<x>
.
So, I condensed ConversionOperationError
&ConversionOperationResult
and into Conversion
. However, if you find that more surprising and would prefer the pair of Error
s/Result
s or have other structure or naming suggestions, I'm open to change it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've refactored to a pair of Error
s & Result
s.
src/result/conversion.rs
Outdated
FromType: ValueTypeExpectation, | ||
ToType: TypeExpectation, | ||
{ | ||
input_value: Box<FromType>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this need to be boxed? Are you concerned about stack size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without boxing, clippy warns:
warning: the `Err`-variant returned from this function is very large
--> src/element/mod.rs:492:34
|
492 | pub fn try_into_int(self) -> ConversionOperationResult<Element, Int> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `Err`-variant is at least 128 bytes
|
= help: try reducing the size of `result::conversion::ConversionOperationError<element::Element, types::integer::Int>`, for example by boxing large elements or replacing it with `Box<result::conversion::ConversionOperationError<element::Element, types::integer::Int>>`
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#result_large_err
= note: `#[warn(clippy::result_large_err)]` on by default
This PR includes a couple changes intended to make working with owned
Element
s easier are not require cloning for 'projecting' an element to be replaced by something it contains.OwnedSequenceIterator
and modifies its implementation to clone lesstry_into_<x>
methods similar toas_<x>
methods onElement
Element
s with the 'failure' case returning the originalElement
Example simplified usage:
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.