Skip to content
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

glib: Add get_transformed to Value #1250

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions glib/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,22 @@ impl Value {
}
}

// rustdoc-stripper-ignore-next
/// Tries to transform the value into the target type
#[doc(alias = "g_value_transform")]
pub fn get_transformed<T: ValueType>(&self) -> Result<T, crate::BoolError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_transformed() maybe? It's a conversion, not a getter.

@pbor what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really more convenient than v.transform().get()?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe if we want a shortcut we should implement TryInto ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to_transformed() maybe? It's a conversion, not a getter.

Yeah I was a bit stumped on naming, get_transformed as least made sense linguistically but I see your point. I guess the best name really would be transform …but we already have that.

Is it really more convenient than v.transform().get()?

Well, that doesn't compile, so …yes?

Maybe if we want a shortcut we should implement TryInto ?

I'd imagine that a TryInto would want to wrap get()? — Seems potentially footgun-y to have try_into() do coercion when get() doesn't, but idk maybe it would be fine.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another naming possibility is transform_to which has a precedent: https://gtk-rs.org/gtk-rs-core/stable/latest/docs/glib/struct.BindingGroupBuilder.html#method.transform_to though that actually returns Option<Value>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other option is to rename this function to transform and rename the other transform function to transform_as_value or something like that

self.transform::<T>().and_then(|v| {
v.get().map_err(|e| {
crate::bool_error!(
"Can't transform value of type '{}' into '{}': {}",
self.type_(),
T::Type::static_type(),
e
)
})
})
}

// rustdoc-stripper-ignore-next
/// Returns `true` if the type of the value corresponds to `T`
/// or is a sub-type of `T`.
Expand Down
Loading