Use impl Into<Option<_>> for functions impacted by nullability fixes

This commit addresses the functions which signatures changed in
commit f9690817 so that users can still use e.g.
`query.set_result(val)` instead of `query.set_result(Some(val))`.

See [1] for an attempt at generalizing this approach.

[1]: https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1133
This commit is contained in:
François Laignel 2022-10-20 18:39:12 +02:00
parent bfbcdb5925
commit 4ebdfb8602
7 changed files with 112 additions and 27 deletions

View file

@ -200,7 +200,7 @@ mod cairo_compositor {
caps
};
q.set_result(Some(&caps));
q.set_result(&caps);
true
}

View file

@ -424,7 +424,7 @@ impl App {
msg.src().map(|s| s.downcast::<gst::Element>().unwrap())
{
let context = gst::Context::new(context_type, true);
context.set_gl_display(Some(&gl_display));
context.set_gl_display(&gl_display);
el.set_context(&context);
}
}

View file

@ -11,7 +11,7 @@ pub trait ContextGLExt {
#[doc(alias = "gst_context_get_gl_display")]
fn gl_display(&self) -> Option<GLDisplay>;
#[doc(alias = "gst_context_set_gl_display")]
fn set_gl_display<T: IsA<GLDisplay>>(&self, display: Option<&T>);
fn set_gl_display<'a, T: IsA<GLDisplay>>(&self, display: impl Into<Option<&'a T>>);
}
impl ContextGLExt for ContextRef {
@ -29,11 +29,11 @@ impl ContextGLExt for ContextRef {
}
}
fn set_gl_display<T: IsA<GLDisplay>>(&self, display: Option<&T>) {
fn set_gl_display<'a, T: IsA<GLDisplay>>(&self, display: impl Into<Option<&'a T>>) {
unsafe {
ffi::gst_context_set_gl_display(
self.as_mut_ptr(),
display.map(|d| d.as_ref()).to_glib_none().0,
display.into().map(|d| d.as_ref()).to_glib_none().0,
);
}
}

View file

@ -413,7 +413,7 @@ mod tests {
use crate::prelude::*;
#[test]
fn test_pool() {
fn pool_with_params() {
crate::init().unwrap();
let pool = crate::BufferPool::new();
@ -436,4 +436,18 @@ mod tests {
pool.set_active(false).unwrap();
}
#[test]
fn pool_no_params() {
crate::init().unwrap();
let pool = crate::BufferPool::new();
let mut config = pool.config();
config.set_params(None, 1024, 0, 2);
pool.set_config(config).unwrap();
pool.set_active(true).unwrap();
let _buf1 = pool.acquire_buffer(None).unwrap();
pool.set_active(false).unwrap();
}
}

View file

@ -965,17 +965,29 @@ impl Uri {
}
#[doc(alias = "gst_query_set_uri")]
pub fn set_uri(&mut self, uri: Option<&str>) {
pub fn set_uri<'a, T>(&mut self, uri: impl Into<Option<&'a T>>)
where
T: 'a + AsRef<str> + ?Sized,
{
unsafe {
ffi::gst_query_set_uri(self.as_mut_ptr(), uri.to_glib_none().0);
ffi::gst_query_set_uri(
self.as_mut_ptr(),
uri.into().map(AsRef::as_ref).to_glib_none().0,
);
}
}
#[doc(alias = "gst_query_set_uri_redirection")]
#[doc(alias = "gst_query_set_uri_redirection_permanent")]
pub fn set_redirection(&mut self, uri: Option<&str>, permanent: bool) {
pub fn set_redirection<'a, T>(&mut self, uri: impl Into<Option<&'a T>>, permanent: bool)
where
T: 'a + AsRef<str> + ?Sized,
{
unsafe {
ffi::gst_query_set_uri_redirection(self.as_mut_ptr(), uri.to_glib_none().0);
ffi::gst_query_set_uri_redirection(
self.as_mut_ptr(),
uri.into().map(AsRef::as_ref).to_glib_none().0,
);
ffi::gst_query_set_uri_redirection_permanent(
self.0.as_mut_ptr(),
permanent.into_glib(),
@ -1453,11 +1465,12 @@ impl Caps {
}
#[doc(alias = "gst_query_set_caps_result")]
pub fn set_result(&mut self, caps: Option<&crate::Caps>) {
pub fn set_result<'a>(&mut self, caps: impl Into<Option<&'a crate::Caps>>) {
unsafe {
ffi::gst_query_set_caps_result(
self.as_mut_ptr(),
caps.map(|caps| caps.as_mut_ptr())
caps.into()
.map(|caps| caps.as_mut_ptr())
.unwrap_or(ptr::null_mut()),
);
}
@ -1527,11 +1540,12 @@ impl Context {
}
#[doc(alias = "gst_query_set_context")]
pub fn set_context(&mut self, context: Option<&crate::Context>) {
pub fn set_context<'a>(&mut self, context: impl Into<Option<&'a crate::Context>>) {
unsafe {
ffi::gst_query_set_context(
self.as_mut_ptr(),
context
.into()
.map(|context| context.as_mut_ptr())
.unwrap_or(ptr::null_mut()),
);
@ -1710,4 +1724,53 @@ mod tests {
let p = Position::new(crate::Format::Time);
assert!(!p.as_mut_ptr().is_null());
}
#[test]
fn allocation_need_pool() {
crate::init().unwrap();
let mut a = Allocation::new(&crate::Caps::builder("foo/bar").build(), true);
let pool = crate::BufferPool::new();
a.add_allocation_pool(Some(&pool), 1024, 1, 4);
}
#[test]
fn allocation_do_not_need_pool() {
crate::init().unwrap();
let mut a = Allocation::new(&crate::Caps::builder("foo/bar").build(), false);
a.add_allocation_pool(crate::BufferPool::NONE, 1024, 1, 4);
// cannot infer type of the type parameter `T` declared on the enum `Option`
//a.add_allocation_pool(None, 1024, 1, 4);
// This would be possible if we moved the `crate::BufferPool`
// as a generic argument instead of using current arg type:
// - `pool: Option<&impl IsA<crate::BufferPool>>`
//a.add_allocation_pool::<crate::BufferPool>(None, 1024, 1, 4);
}
#[test]
fn set_uri() {
crate::init().unwrap();
let mut uri_q = Uri::new();
uri_q.set_uri("https://test.org");
uri_q.set_uri(&String::from("https://test.org"));
uri_q.set_uri(Some("https://test.org"));
uri_q.set_uri(Some(&String::from("https://test.org")));
// FIXME: this is commented out for now due to an inconsistent
// assertion in `GStreamer` which results in critical logs.
/*
let none: Option<&str> = None;
uri_q.set_uri(none);
let none: Option<String> = None;
uri_q.set_uri(none.as_ref());
uri_q.set_uri::<str>(None);
*/
}
}

View file

@ -58,11 +58,12 @@ impl TocRef {
}
#[doc(alias = "gst_toc_set_tags")]
pub fn set_tags(&mut self, tag_list: Option<TagList>) {
pub fn set_tags(&mut self, tag_list: impl Into<Option<TagList>>) {
unsafe {
ffi::gst_toc_set_tags(
self.as_mut_ptr(),
tag_list
.into()
.map(|t| t.into_glib_ptr())
.unwrap_or(ptr::null_mut()),
);
@ -70,11 +71,14 @@ impl TocRef {
}
#[doc(alias = "gst_toc_merge_tags")]
pub fn merge_tags(&mut self, tag_list: Option<&TagList>, mode: TagMergeMode) {
pub fn merge_tags<'a>(&mut self, tag_list: impl Into<Option<&'a TagList>>, mode: TagMergeMode) {
unsafe {
ffi::gst_toc_merge_tags(
self.as_mut_ptr(),
tag_list.map(|l| l.as_mut_ptr()).unwrap_or(ptr::null_mut()),
tag_list
.into()
.map(|l| l.as_mut_ptr())
.unwrap_or(ptr::null_mut()),
mode.into_glib(),
);
}
@ -192,11 +196,12 @@ impl TocEntryRef {
}
#[doc(alias = "gst_toc_entry_set_tags")]
pub fn set_tags(&mut self, tag_list: Option<TagList>) {
pub fn set_tags(&mut self, tag_list: impl Into<Option<TagList>>) {
unsafe {
ffi::gst_toc_entry_set_tags(
self.as_mut_ptr(),
tag_list
.into()
.map(|t| t.into_glib_ptr())
.unwrap_or(ptr::null_mut()),
);
@ -204,11 +209,14 @@ impl TocEntryRef {
}
#[doc(alias = "gst_toc_entry_merge_tags")]
pub fn merge_tags(&mut self, tag_list: Option<&TagList>, mode: TagMergeMode) {
pub fn merge_tags<'a>(&mut self, tag_list: impl Into<Option<&'a TagList>>, mode: TagMergeMode) {
unsafe {
ffi::gst_toc_entry_merge_tags(
self.as_mut_ptr(),
tag_list.map(|l| l.as_mut_ptr()).unwrap_or(ptr::null_mut()),
tag_list
.into()
.map(|l| l.as_mut_ptr())
.unwrap_or(ptr::null_mut()),
mode.into_glib(),
);
}

View file

@ -135,7 +135,7 @@ mod tests {
tags.get_mut()
.unwrap()
.add::<Title>(&"toc", TagMergeMode::Append);
toc.set_tags(Some(tags));
toc.set_tags(tags);
let mut toc_edition = TocEntry::new(TocEntryType::Edition, "edition");
{
@ -154,7 +154,7 @@ mod tests {
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 1.1", TagMergeMode::Append);
toc_chap_1_1.set_tags(Some(tags));
toc_chap_1_1.set_tags(tags);
}
toc_chap_1.append_sub_entry(toc_chap_1_1);
@ -166,7 +166,7 @@ mod tests {
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 1.2", TagMergeMode::Append);
toc_chap_1_2.set_tags(Some(tags));
toc_chap_1_2.set_tags(tags);
}
toc_chap_1.append_sub_entry(toc_chap_1_2);
}
@ -180,7 +180,7 @@ mod tests {
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 2", TagMergeMode::Append);
toc_chap_2.set_tags(Some(tags));
toc_chap_2.set_tags(tags);
}
toc_edition.append_sub_entry(toc_chap_2);
}
@ -415,7 +415,7 @@ mod tests {
tags.get_mut()
.unwrap()
.add::<Title>(&"toc", TagMergeMode::Append);
toc.set_tags(Some(tags));
toc.set_tags(tags);
let mut toc_edition = TocEntry::new(TocEntryType::Edition, "edition");
{
@ -434,7 +434,7 @@ mod tests {
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 1.1", TagMergeMode::Append);
toc_chap_1_1.set_tags(Some(tags));
toc_chap_1_1.set_tags(tags);
}
toc_chap_1.append_sub_entry(toc_chap_1_1);
@ -446,7 +446,7 @@ mod tests {
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 1.2", TagMergeMode::Append);
toc_chap_1_2.set_tags(Some(tags));
toc_chap_1_2.set_tags(tags);
}
toc_chap_1.append_sub_entry(toc_chap_1_2);
}
@ -460,7 +460,7 @@ mod tests {
tags.get_mut()
.unwrap()
.add::<Title>(&"chapter 2", TagMergeMode::Append);
toc_chap_2.set_tags(Some(tags));
toc_chap_2.set_tags(tags);
}
toc_edition.append_sub_entry(toc_chap_2);
}