Skip to content
Snippets Groups Projects

Draft: Use `imp Into<Option<_>>` in argument position as much as possible

This allows users to call the corresponding functions without the need to embed the argument in Some(_).

I applied this approach to manual code and I moved Bin::new and Pipeline::new to manual code to implement it too there. Many auto functions could take advantage of this but that would require a gir evolution. We could probably apply this to App{Sink,Src}::set_caps manually though.

See the result in the examples and in this (WIP) gst-plugins-rs branch: https://gitlab.freedesktop.org/fengalin/gst-plugins-rs/-/tree/nullability-into-option-args

When the user wants to pass None, most of the time, the compiler is able to infer the type:

    let buf = pool.acquire_buffer(None).unwrap();

However, when the function uses a trait bound for the optional argument type, user must disambiguate the type. Ex.:

    alloc_query.add_allocation_pool::<gst::BufferPool>(None, 1024, 1, 4);

For most types, the NONE constant is also available:

    alloc_query.add_allocation_pool(gst::BufferPool::NONE, 1024, 1, 4);
Edited by François Laignel

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • 30 30
    31 31 impl Bin {
    32 32 pub const NONE: Option<&'static Bin> = None;
    33
    34 #[doc(alias = "gst_bin_new")]
    35 pub fn new(name: Option<&str>) -> Bin {
    • This seems like something that should be done at the code generator layer

    • Right. See my comment in the MR message.

    • Based on my discussion on Matrix, let's get the generator fixed then and get this specific change in right after the release.

      Fixing the code generator should be a matter of reverting one of my PRs from a couple of years ago when we went the opposite direction because the compiler was making usage of such code unnecessarily hard. That seems to have been solved in the compiler in the meantime.

    • Please register or sign in to reply
  • I still need to get everything right in gst-plugins-rs!

  • 12 12 use std::ptr;
    13 13
    14 14 pub trait DeviceImpl: DeviceImplExt + GstObjectImpl + Send + Sync {
    15 fn create_element(&self, name: Option<&str>) -> Result<Element, LoggableError> {
    15 fn create_element<'a>(
    16 &self,
    17 name: impl Into<Option<&'a str>>,
    • I think for all the Impl traits this doesn't make much sense. We are the caller and we already define the type there, this just makes the implementation of the functions a bit more complicated for the user

    • Please register or sign in to reply
  • 22 25 }
    23 26
    24 27 pub trait DeviceImplExt: ObjectSubclass {
    25 fn parent_create_element(&self, name: Option<&str>) -> Result<Element, LoggableError>;
    28 fn parent_create_element<'a>(
    29 &self,
    30 name: impl Into<Option<&'a str>>,
  • added 1 commit

    • d032e721 - Use `imp Into<Option<_>>` in argument position as much as possible

    Compare with previous version

  • 105 105 }
    106 106
    107 107 #[doc(alias = "gst_bus_create_watch")]
    108 pub fn create_watch<F>(&self, name: Option<&str>, priority: Priority, func: F) -> glib::Source
    108 pub fn create_watch<'a, F>(
    109 &self,
    110 name: impl Into<Option<&'a str>>,
  • 131 131
    132 132 // rustdoc-stripper-ignore-next
    133 133 /// Default: `-i32::MAX/1`
    134 134 pub fn minimum(mut self, minimum: crate::Fraction) -> Self {
  • François Laignel mentioned in merge request !1134 (merged)

    mentioned in merge request !1134 (merged)

  • François Laignel marked this merge request as draft

    marked this merge request as draft

  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Loading
  • Please register or sign in to reply
    Loading