-
Notifications
You must be signed in to change notification settings - Fork 89
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
Using both try_setter
and setter(strip_option)
does not compile
#284
Comments
Can you provide the output of running |
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::rust_2021::*;
#[macro_use]
extern crate std;
struct Example {
#[builder(try_setter, setter(strip_option))]
value: Option<String>,
}
#[allow(clippy::all)]
/**Builder for [`Example`](struct.Example.html).
*/
struct ExampleBuilder {
value: ::derive_builder::export::core::option::Option<Option<String>>,
}
#[automatically_derived]
#[allow(clippy::all)]
impl ::core::clone::Clone for ExampleBuilder {
#[inline]
fn clone(&self) -> ExampleBuilder {
ExampleBuilder {
value: ::core::clone::Clone::clone(&self.value),
}
}
}
#[allow(clippy::all)]
#[allow(dead_code)]
impl ExampleBuilder {
#[allow(unused_mut)]
pub fn value(&mut self, value: String) -> &mut Self {
let mut new = self;
new
.value = ::derive_builder::export::core::option::Option::Some(
::derive_builder::export::core::option::Option::Some(value),
);
new
}
pub fn try_value<VALUE: ::derive_builder::export::core::convert::TryInto<String>>(
&mut self,
value: VALUE,
) -> ::derive_builder::export::core::result::Result<&mut Self, VALUE::Error> {
let converted: String = value.try_into()?;
let mut new = self;
new.value = ::derive_builder::export::core::option::Option::Some(converted);
Ok(new)
}
/**Builds a new `Example`.
# Errors
If a required field has not been initialized.
*/
fn build(
&self,
) -> ::derive_builder::export::core::result::Result<Example, ExampleBuilderError> {
Ok(Example {
value: match self.value {
Some(ref value) => {
::derive_builder::export::core::clone::Clone::clone(value)
}
None => {
return ::derive_builder::export::core::result::Result::Err(
::derive_builder::export::core::convert::Into::into(
::derive_builder::UninitializedFieldError::from("value"),
),
);
}
},
})
}
/// Create an empty builder, with all fields set to `None` or `PhantomData`.
fn create_empty() -> Self {
Self {
value: ::derive_builder::export::core::default::Default::default(),
}
}
}
impl ::derive_builder::export::core::default::Default for ExampleBuilder {
fn default() -> Self {
Self::create_empty()
}
}
///Error type for ExampleBuilder
#[non_exhaustive]
enum ExampleBuilderError {
/// Uninitialized field
UninitializedField(&'static str),
/// Custom validation error
ValidationError(::derive_builder::export::core::string::String),
}
#[automatically_derived]
impl ::core::fmt::Debug for ExampleBuilderError {
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
ExampleBuilderError::UninitializedField(__self_0) => {
::core::fmt::Formatter::debug_tuple_field1_finish(
f,
"UninitializedField",
&__self_0,
)
}
ExampleBuilderError::ValidationError(__self_0) => {
::core::fmt::Formatter::debug_tuple_field1_finish(
f,
"ValidationError",
&__self_0,
)
}
}
}
}
impl ::derive_builder::export::core::convert::From<
::derive_builder::UninitializedFieldError,
> for ExampleBuilderError {
fn from(s: ::derive_builder::UninitializedFieldError) -> Self {
Self::UninitializedField(s.field_name())
}
}
impl ::derive_builder::export::core::convert::From<
::derive_builder::export::core::string::String,
> for ExampleBuilderError {
fn from(s: ::derive_builder::export::core::string::String) -> Self {
Self::ValidationError(s)
}
}
impl ::derive_builder::export::core::fmt::Display for ExampleBuilderError {
fn fmt(
&self,
f: &mut ::derive_builder::export::core::fmt::Formatter,
) -> ::derive_builder::export::core::fmt::Result {
match self {
Self::UninitializedField(ref field) => {
f
.write_fmt(
::core::fmt::Arguments::new_v1(
&["`", "` must be initialized"],
&[::core::fmt::ArgumentV1::new_display(&field)],
),
)
}
Self::ValidationError(ref error) => {
f
.write_fmt(
::core::fmt::Arguments::new_v1(
&[""],
&[::core::fmt::ArgumentV1::new_display(&error)],
),
)
}
}
}
}
impl std::error::Error for ExampleBuilderError {} |
The issue here appears to be an ambiguity about the scope of Some of the code seems to think that However, immediately after that (here), the possibility of Fixing this in either direction is straightforward, but I'm not sure what people's expected behavior here would be; should |
Would adding a To answer your question though, if I have a as a temporary stopgap, i rolled those methods by hand, e.g.: pub fn try_jwt_id<T: TryInto<String>>(&mut self, jwt_id: T) -> Result<&mut Self, T::Error> {
Ok(self.jwt_id(jwt_id.try_into()?))
} |
I think it would be better to have the try_setter inherit the value of Since this code previously wouldn't compile, we know that choosing to make it honor Therefore, the change here is to:
If you're interested in making those updates I'm happy to review and merge them. |
Sounds good. My CI is currently blocked by #285 so that would take precedence for me. I will circle back to this at some point and work on it though. Thank you for your help on this and for the crate. It is quite helpful! |
Using both
try_setter
andsetter(trip_option)
results in the error expected enumOption
, found structString
.version:
0.12.0
repro:
https://github.com/chanced/derive_builder_issue
The text was updated successfully, but these errors were encountered: