From 9e2c6268cbfea2f531c97bfb0bfe32966970fe95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Tue, 21 May 2024 16:05:48 +0300 Subject: [PATCH] gstreamer: Use usize instead of u32 for various indices This affects buffers, caps, caps features, structures and tag lists and makes it easier to use them with other Rust APIs that use usize-based indicing. Part-of: --- gstreamer/src/buffer.rs | 111 +++++++++++++++------------------ gstreamer/src/buffer_cursor.rs | 10 +-- gstreamer/src/bufferlist.rs | 69 +++++++++++--------- gstreamer/src/caps.rs | 56 +++++++++-------- gstreamer/src/caps_features.rs | 14 ++--- gstreamer/src/structure.rs | 22 ++++--- gstreamer/src/tags.rs | 78 ++++++++++------------- gstreamer/src/tags_serde.rs | 2 +- 8 files changed, 183 insertions(+), 179 deletions(-) diff --git a/gstreamer/src/buffer.rs b/gstreamer/src/buffer.rs index 1f6c692f7..11277ce15 100644 --- a/gstreamer/src/buffer.rs +++ b/gstreamer/src/buffer.rs @@ -209,9 +209,10 @@ impl BufferRef { fn memory_range_into_idx_len( &self, - range: impl RangeBounds, + range: impl RangeBounds, ) -> Result<(u32, i32), glib::BoolError> { let n_memory = self.n_memory(); + debug_assert!(n_memory <= u32::MAX as usize); let start_idx = match range.start_bound() { ops::Bound::Included(idx) if *idx >= n_memory => { @@ -238,7 +239,7 @@ impl BufferRef { }; Ok(( - start_idx, + start_idx as u32, i32::try_from(end_idx - start_idx).map_err(|_| glib::bool_error!("Too large range"))?, )) } @@ -247,7 +248,7 @@ impl BufferRef { #[inline] pub fn map_range_readable( &self, - range: impl RangeBounds, + range: impl RangeBounds, ) -> Result, glib::BoolError> { let (idx, len) = self.memory_range_into_idx_len(range)?; unsafe { @@ -275,7 +276,7 @@ impl BufferRef { #[inline] pub fn map_range_writable( &mut self, - range: impl RangeBounds, + range: impl RangeBounds, ) -> Result, glib::BoolError> { let (idx, len) = self.memory_range_into_idx_len(range)?; unsafe { @@ -656,7 +657,7 @@ impl BufferRef { } #[doc(alias = "gst_buffer_find_memory")] - pub fn find_memory(&self, range: impl RangeBounds) -> Option<(Range, usize)> { + pub fn find_memory(&self, range: impl RangeBounds) -> Option<(Range, usize)> { let (offset, size) = self.byte_range_into_offset_len(range).ok()?; unsafe { @@ -674,8 +675,8 @@ impl BufferRef { )); if res { - let idx = idx.assume_init(); - let length = length.assume_init(); + let idx = idx.assume_init() as usize; + let length = length.assume_init() as usize; let skip = skip.assume_init(); Some((idx..(idx + length), skip)) } else { @@ -687,56 +688,43 @@ impl BufferRef { #[doc(alias = "get_all_memory")] #[doc(alias = "gst_buffer_get_all_memory")] pub fn all_memory(&self) -> Option { - unsafe { - let res = ffi::gst_buffer_get_all_memory(self.as_mut_ptr()); - if res.is_null() { - None - } else { - Some(from_glib_full(res)) - } - } + unsafe { from_glib_full(ffi::gst_buffer_get_all_memory(self.as_mut_ptr())) } } #[doc(alias = "get_max_memory")] #[doc(alias = "gst_buffer_get_max_memory")] - pub fn max_memory() -> u32 { - unsafe { ffi::gst_buffer_get_max_memory() } + pub fn max_memory() -> usize { + unsafe { ffi::gst_buffer_get_max_memory() as usize } } #[doc(alias = "get_memory")] #[doc(alias = "gst_buffer_get_memory")] - pub fn memory(&self, idx: u32) -> Option { + pub fn memory(&self, idx: usize) -> Option { if idx >= self.n_memory() { - None - } else { - unsafe { - let res = ffi::gst_buffer_get_memory(self.as_mut_ptr(), idx); - if res.is_null() { - None - } else { - Some(from_glib_full(res)) - } - } + return None; + } + unsafe { + let res = ffi::gst_buffer_get_memory(self.as_mut_ptr(), idx as u32); + Some(from_glib_full(res)) } } #[doc(alias = "get_memory_range")] #[doc(alias = "gst_buffer_get_memory_range")] - pub fn memory_range(&self, range: impl RangeBounds) -> Option { + pub fn memory_range(&self, range: impl RangeBounds) -> Option { let (idx, len) = self.memory_range_into_idx_len(range).ok()?; unsafe { let res = ffi::gst_buffer_get_memory_range(self.as_mut_ptr(), idx, len); - if res.is_null() { - None - } else { - Some(from_glib_full(res)) - } + from_glib_full(res) } } #[doc(alias = "gst_buffer_insert_memory")] - pub fn insert_memory(&mut self, idx: u32, mem: Memory) { + pub fn insert_memory(&mut self, idx: impl Into>, mem: Memory) { + let n_memory = self.n_memory(); + let idx = idx.into(); + let idx = idx.unwrap_or(n_memory); assert!(idx <= self.n_memory()); unsafe { ffi::gst_buffer_insert_memory(self.as_mut_ptr(), idx as i32, mem.into_glib_ptr()) } } @@ -747,7 +735,7 @@ impl BufferRef { } #[doc(alias = "gst_buffer_is_memory_range_writable")] - pub fn is_memory_range_writable(&self, range: impl RangeBounds) -> bool { + pub fn is_memory_range_writable(&self, range: impl RangeBounds) -> bool { let Some((idx, len)) = self.memory_range_into_idx_len(range).ok() else { return false; }; @@ -762,28 +750,25 @@ impl BufferRef { } #[doc(alias = "gst_buffer_n_memory")] - pub fn n_memory(&self) -> u32 { - unsafe { ffi::gst_buffer_n_memory(self.as_ptr() as *mut _) } + pub fn n_memory(&self) -> usize { + unsafe { ffi::gst_buffer_n_memory(self.as_ptr() as *mut _) as usize } } #[doc(alias = "gst_buffer_peek_memory")] - pub fn peek_memory(&self, idx: u32) -> &MemoryRef { + pub fn peek_memory(&self, idx: usize) -> &MemoryRef { assert!(idx < self.n_memory()); - unsafe { MemoryRef::from_ptr(ffi::gst_buffer_peek_memory(self.as_mut_ptr(), idx)) } + unsafe { MemoryRef::from_ptr(ffi::gst_buffer_peek_memory(self.as_mut_ptr(), idx as u32)) } } #[doc(alias = "gst_buffer_peek_memory")] - pub fn peek_memory_mut(&mut self, idx: u32) -> Result<&mut MemoryRef, glib::BoolError> { + pub fn peek_memory_mut(&mut self, idx: usize) -> Result<&mut MemoryRef, glib::BoolError> { assert!(idx < self.n_memory()); unsafe { - let mem = ffi::gst_buffer_peek_memory(self.as_mut_ptr(), idx); + let mem = ffi::gst_buffer_peek_memory(self.as_mut_ptr(), idx as u32); if ffi::gst_mini_object_is_writable(mem as *mut _) == glib::ffi::GFALSE { Err(glib::bool_error!("Memory not writable")) } else { - Ok(MemoryRef::from_mut_ptr(ffi::gst_buffer_peek_memory( - self.as_mut_ptr(), - idx, - ))) + Ok(MemoryRef::from_mut_ptr(mem)) } } } @@ -799,13 +784,13 @@ impl BufferRef { } #[doc(alias = "gst_buffer_remove_memory")] - pub fn remove_memory(&mut self, idx: u32) { + pub fn remove_memory(&mut self, idx: usize) { assert!(idx < self.n_memory()); - unsafe { ffi::gst_buffer_remove_memory(self.as_mut_ptr(), idx) } + unsafe { ffi::gst_buffer_remove_memory(self.as_mut_ptr(), idx as u32) } } #[doc(alias = "gst_buffer_remove_memory_range")] - pub fn remove_memory_range(&mut self, range: impl RangeBounds) { + pub fn remove_memory_range(&mut self, range: impl RangeBounds) { let (idx, len) = self .memory_range_into_idx_len(range) .expect("Invalid memory range"); @@ -819,13 +804,15 @@ impl BufferRef { } #[doc(alias = "gst_buffer_replace_memory")] - pub fn replace_memory(&mut self, idx: u32, mem: Memory) { + pub fn replace_memory(&mut self, idx: usize, mem: Memory) { assert!(idx < self.n_memory()); - unsafe { ffi::gst_buffer_replace_memory(self.as_mut_ptr(), idx, mem.into_glib_ptr()) } + unsafe { + ffi::gst_buffer_replace_memory(self.as_mut_ptr(), idx as u32, mem.into_glib_ptr()) + } } #[doc(alias = "gst_buffer_replace_memory_range")] - pub fn replace_memory_range(&mut self, range: impl RangeBounds, mem: Memory) { + pub fn replace_memory_range(&mut self, range: impl RangeBounds, mem: Memory) { let (idx, len) = self .memory_range_into_idx_len(range) .expect("Invalid memory range"); @@ -984,7 +971,7 @@ macro_rules! define_iter( $name { buffer, idx: 0, - n_memory: n_memory as usize, + n_memory, } } } @@ -1000,7 +987,7 @@ macro_rules! define_iter( #[allow(unused_unsafe)] unsafe { - let item = $get_item(self.buffer, self.idx as u32).unwrap(); + let item = $get_item(self.buffer, self.idx).unwrap(); self.idx += 1; Some(item) } @@ -1025,7 +1012,7 @@ macro_rules! define_iter( #[allow(unused_unsafe)] unsafe { self.idx = end + 1; - Some($get_item(self.buffer, end as u32).unwrap()) + Some($get_item(self.buffer, end).unwrap()) } } } @@ -1036,7 +1023,7 @@ macro_rules! define_iter( } else { #[allow(unused_unsafe)] unsafe { - Some($get_item(self.buffer, self.n_memory as u32 - 1).unwrap()) + Some($get_item(self.buffer, self.n_memory - 1).unwrap()) } } } @@ -1052,7 +1039,7 @@ macro_rules! define_iter( #[allow(unused_unsafe)] unsafe { self.n_memory -= 1; - Some($get_item(self.buffer, self.n_memory as u32).unwrap()) + Some($get_item(self.buffer, self.n_memory).unwrap()) } } @@ -1065,7 +1052,7 @@ macro_rules! define_iter( #[allow(unused_unsafe)] unsafe { self.n_memory = end - 1; - Some($get_item(self.buffer, self.n_memory as u32).unwrap()) + Some($get_item(self.buffer, self.n_memory).unwrap()) } } } @@ -1082,7 +1069,7 @@ define_iter!( &'a BufferRef, &'a MemoryRef, |buffer: &BufferRef, idx| { - let ptr = ffi::gst_buffer_peek_memory(buffer.as_mut_ptr(), idx); + let ptr = ffi::gst_buffer_peek_memory(buffer.as_mut_ptr(), idx as u32); if ptr.is_null() { None } else { @@ -1096,7 +1083,7 @@ define_iter!( &'a mut BufferRef, &'a mut MemoryRef, |buffer: &mut BufferRef, idx| { - let ptr = ffi::gst_buffer_peek_memory(buffer.as_mut_ptr(), idx); + let ptr = ffi::gst_buffer_peek_memory(buffer.as_mut_ptr(), idx as u32); if ptr.is_null() { None } else { @@ -1457,8 +1444,8 @@ pub struct Dump<'a> { struct BufferChunked16Iter<'a> { buffer: &'a BufferRef, - mem_idx: u32, - mem_len: u32, + mem_idx: usize, + mem_len: usize, map: Option>, map_offset: usize, len: usize, diff --git a/gstreamer/src/buffer_cursor.rs b/gstreamer/src/buffer_cursor.rs index 727f9fe2c..f00c1eb5f 100644 --- a/gstreamer/src/buffer_cursor.rs +++ b/gstreamer/src/buffer_cursor.rs @@ -10,8 +10,8 @@ use crate::{ pub struct BufferCursor { buffer: Option, size: u64, - num_mem: u32, - cur_mem_idx: u32, + num_mem: usize, + cur_mem_idx: usize, cur_offset: u64, cur_mem_offset: usize, map_info: ffi::GstMapInfo, @@ -21,8 +21,8 @@ pub struct BufferCursor { pub struct BufferRefCursor { buffer: T, size: u64, - num_mem: u32, - cur_mem_idx: u32, + num_mem: usize, + cur_mem_idx: usize, cur_offset: u64, cur_mem_offset: usize, map_info: ffi::GstMapInfo, @@ -112,7 +112,7 @@ macro_rules! define_read_write_fn_impl( let buffer_ref: fn(&Self) -> &BufferRef = $get_buffer_ref; let memory = ffi::gst_buffer_peek_memory( buffer_ref($self).as_mut_ptr(), - $self.cur_mem_idx, + $self.cur_mem_idx as u32, ); debug_assert!(!memory.is_null()); diff --git a/gstreamer/src/bufferlist.rs b/gstreamer/src/bufferlist.rs index 98d6c5bf8..8f30f5820 100644 --- a/gstreamer/src/bufferlist.rs +++ b/gstreamer/src/bufferlist.rs @@ -1,7 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. use std::{ - fmt, + cmp, fmt, ops::{ControlFlow, RangeBounds}, ptr, }; @@ -24,21 +24,26 @@ impl BufferList { #[doc(alias = "gst_buffer_list_new_sized")] pub fn new_sized(size: usize) -> Self { assert_initialized_main_thread!(); - unsafe { from_glib_full(ffi::gst_buffer_list_new_sized(size as u32)) } + unsafe { from_glib_full(ffi::gst_buffer_list_new_sized(u32::try_from(size).unwrap())) } } } impl BufferListRef { #[doc(alias = "gst_buffer_list_insert")] - pub fn insert(&mut self, idx: i32, buffer: Buffer) { + pub fn insert(&mut self, idx: impl Into>, buffer: Buffer) { unsafe { + let len = self.len(); + debug_assert!(len <= u32::MAX as usize); + + let idx = idx.into(); + let idx = cmp::min(idx.unwrap_or(len), len) as i32; ffi::gst_buffer_list_insert(self.as_mut_ptr(), idx, buffer.into_glib_ptr()); } } #[doc(alias = "gst_buffer_list_add")] pub fn add(&mut self, buffer: Buffer) { - self.insert(-1, buffer); + self.insert(None, buffer); } #[doc(alias = "gst_buffer_list_copy_deep")] @@ -47,8 +52,10 @@ impl BufferListRef { } #[doc(alias = "gst_buffer_list_remove")] - pub fn remove(&mut self, range: impl RangeBounds) { - let n = self.len() as u32; + pub fn remove(&mut self, range: impl RangeBounds) { + let n = self.len(); + debug_assert!(n <= u32::MAX as usize); + let start_idx = match range.start_bound() { std::ops::Bound::Included(idx) => *idx, std::ops::Bound::Excluded(idx) => idx.checked_add(1).unwrap(), @@ -63,39 +70,45 @@ impl BufferListRef { }; assert!(end_idx <= n); - unsafe { ffi::gst_buffer_list_remove(self.as_mut_ptr(), start_idx, end_idx - start_idx) } + unsafe { + ffi::gst_buffer_list_remove( + self.as_mut_ptr(), + start_idx as u32, + (end_idx - start_idx) as u32, + ) + } } #[doc(alias = "gst_buffer_list_get")] - pub fn get(&self, idx: u32) -> Option<&BufferRef> { + pub fn get(&self, idx: usize) -> Option<&BufferRef> { unsafe { - if idx as usize >= self.len() { + if idx >= self.len() { return None; } - let ptr = ffi::gst_buffer_list_get(self.as_mut_ptr(), idx); + let ptr = ffi::gst_buffer_list_get(self.as_mut_ptr(), idx as u32); Some(BufferRef::from_ptr(ptr)) } } #[doc(alias = "gst_buffer_list_get")] - pub fn get_owned(&self, idx: u32) -> Option { + pub fn get_owned(&self, idx: usize) -> Option { unsafe { - if idx as usize >= self.len() { + if idx >= self.len() { return None; } - let ptr = ffi::gst_buffer_list_get(self.as_mut_ptr(), idx); + let ptr = ffi::gst_buffer_list_get(self.as_mut_ptr(), idx as u32); Some(from_glib_none(ptr)) } } #[doc(alias = "gst_buffer_list_get_writable")] #[doc(alias = "get_writable")] - pub fn get_mut(&mut self, idx: u32) -> Option<&mut BufferRef> { + pub fn get_mut(&mut self, idx: usize) -> Option<&mut BufferRef> { unsafe { - if idx as usize >= self.len() { + if idx >= self.len() { return None; } - let ptr = ffi::gst_buffer_list_get_writable(self.as_mut_ptr(), idx); + let ptr = ffi::gst_buffer_list_get_writable(self.as_mut_ptr(), idx as u32); Some(BufferRef::from_mut_ptr(ptr)) } } @@ -123,14 +136,14 @@ impl BufferListRef { } #[doc(alias = "gst_buffer_list_foreach")] - pub fn foreach ControlFlow<(), ()>>(&self, func: F) -> bool { - unsafe extern "C" fn trampoline ControlFlow<(), ()>>( + pub fn foreach ControlFlow<(), ()>>(&self, func: F) -> bool { + unsafe extern "C" fn trampoline ControlFlow<(), ()>>( buffer: *mut *mut ffi::GstBuffer, idx: u32, user_data: glib::ffi::gpointer, ) -> glib::ffi::gboolean { let func = user_data as *mut F; - let res = (*func)(&Buffer::from_glib_borrow(*buffer), idx); + let res = (*func)(&Buffer::from_glib_borrow(*buffer), idx as usize); matches!(res, ControlFlow::Continue(_)).into_glib() } @@ -147,12 +160,12 @@ impl BufferListRef { } #[doc(alias = "gst_buffer_list_foreach")] - pub fn foreach_mut ControlFlow, Option>>( + pub fn foreach_mut ControlFlow, Option>>( &mut self, func: F, ) -> bool { unsafe extern "C" fn trampoline< - F: FnMut(Buffer, u32) -> ControlFlow, Option>, + F: FnMut(Buffer, usize) -> ControlFlow, Option>, >( buffer: *mut *mut ffi::GstBuffer, idx: u32, @@ -164,7 +177,7 @@ impl BufferListRef { buffer as *mut *const ffi::GstBuffer, ptr::null_mut::(), )), - idx, + idx as usize, ); let (cont, res_buffer) = match res { @@ -243,7 +256,7 @@ macro_rules! define_iter( $name { list, idx: 0, - size: list.len() as usize, + size: list.len(), } } } @@ -257,7 +270,7 @@ macro_rules! define_iter( return None; } - let item = $get_item(self.list, self.idx as u32).unwrap(); + let item = $get_item(self.list, self.idx).unwrap(); self.idx += 1; Some(item) @@ -280,7 +293,7 @@ macro_rules! define_iter( None } else { self.idx = end + 1; - Some($get_item(self.list, end as u32).unwrap()) + Some($get_item(self.list, end).unwrap()) } } @@ -288,7 +301,7 @@ macro_rules! define_iter( if self.idx == self.size { None } else { - Some($get_item(self.list, self.size as u32 - 1).unwrap()) + Some($get_item(self.list, self.size - 1).unwrap()) } } } @@ -301,7 +314,7 @@ macro_rules! define_iter( } self.size -= 1; - Some($get_item(self.list, self.size as u32).unwrap()) + Some($get_item(self.list, self.size).unwrap()) } fn nth_back(&mut self, n: usize) -> Option { @@ -311,7 +324,7 @@ macro_rules! define_iter( None } else { self.size = end - 1; - Some($get_item(self.list, self.size as u32).unwrap()) + Some($get_item(self.list, self.size).unwrap()) } } } diff --git a/gstreamer/src/caps.rs b/gstreamer/src/caps.rs index 0e9fcaf32..756c47216 100644 --- a/gstreamer/src/caps.rs +++ b/gstreamer/src/caps.rs @@ -316,13 +316,13 @@ impl CapsRef { #[doc(alias = "get_structure")] #[doc(alias = "gst_caps_get_structure")] - pub fn structure(&self, idx: u32) -> Option<&StructureRef> { + pub fn structure(&self, idx: usize) -> Option<&StructureRef> { if idx >= self.size() { return None; } unsafe { - let structure = ffi::gst_caps_get_structure(self.as_ptr(), idx); + let structure = ffi::gst_caps_get_structure(self.as_ptr(), idx as u32); if structure.is_null() { return None; } @@ -333,13 +333,13 @@ impl CapsRef { #[doc(alias = "get_mut_structure")] #[doc(alias = "gst_caps_get_structure")] - pub fn structure_mut(&mut self, idx: u32) -> Option<&mut StructureRef> { + pub fn structure_mut(&mut self, idx: usize) -> Option<&mut StructureRef> { if idx >= self.size() { return None; } unsafe { - let structure = ffi::gst_caps_get_structure(self.as_ptr(), idx); + let structure = ffi::gst_caps_get_structure(self.as_ptr(), idx as u32); if structure.is_null() { return None; } @@ -350,38 +350,38 @@ impl CapsRef { #[doc(alias = "get_features")] #[doc(alias = "gst_caps_get_features")] - pub fn features(&self, idx: u32) -> Option<&CapsFeaturesRef> { + pub fn features(&self, idx: usize) -> Option<&CapsFeaturesRef> { if idx >= self.size() { return None; } unsafe { - let features = ffi::gst_caps_get_features(self.as_ptr(), idx); + let features = ffi::gst_caps_get_features(self.as_ptr(), idx as u32); Some(CapsFeaturesRef::from_glib_borrow(features)) } } #[doc(alias = "get_mut_features")] #[doc(alias = "gst_caps_get_features")] - pub fn features_mut(&mut self, idx: u32) -> Option<&mut CapsFeaturesRef> { + pub fn features_mut(&mut self, idx: usize) -> Option<&mut CapsFeaturesRef> { if idx >= self.size() { return None; } unsafe { - let features = ffi::gst_caps_get_features(self.as_ptr(), idx); + let features = ffi::gst_caps_get_features(self.as_ptr(), idx as u32); Some(CapsFeaturesRef::from_glib_borrow_mut(features)) } } #[doc(alias = "gst_caps_set_features")] - pub fn set_features(&mut self, idx: u32, features: Option) { + pub fn set_features(&mut self, idx: usize, features: Option) { assert!(idx < self.size()); unsafe { ffi::gst_caps_set_features( self.as_mut_ptr(), - idx, + idx as u32, features .map(|f| f.into_glib_ptr()) .unwrap_or(ptr::null_mut()), @@ -405,8 +405,12 @@ impl CapsRef { #[doc(alias = "get_size")] #[doc(alias = "gst_caps_get_size")] - pub fn size(&self) -> u32 { - unsafe { ffi::gst_caps_get_size(self.as_ptr()) } + pub fn size(&self) -> usize { + unsafe { ffi::gst_caps_get_size(self.as_ptr()) as usize } + } + + pub fn len(&self) -> usize { + self.size() } pub fn iter(&self) -> Iter { @@ -444,8 +448,10 @@ impl CapsRef { } #[doc(alias = "gst_caps_remove_structure")] - pub fn remove_structure(&mut self, idx: u32) { - unsafe { ffi::gst_caps_remove_structure(self.as_mut_ptr(), idx) } + pub fn remove_structure(&mut self, idx: usize) { + assert!(idx < self.size()); + + unsafe { ffi::gst_caps_remove_structure(self.as_mut_ptr(), idx as u32) } } #[doc(alias = "gst_caps_append")] @@ -704,7 +710,7 @@ macro_rules! define_iter( } unsafe { - let item = $get_item(self.caps, self.idx as u32).unwrap(); + let item = $get_item(self.caps, self.idx).unwrap(); self.idx += 1; Some(item) } @@ -728,7 +734,7 @@ macro_rules! define_iter( } else { unsafe { self.idx = end + 1; - Some($get_item(self.caps, end as u32).unwrap()) + Some($get_item(self.caps, end).unwrap()) } } } @@ -738,7 +744,7 @@ macro_rules! define_iter( None } else { unsafe { - Some($get_item(self.caps, self.n_structures as u32 - 1).unwrap()) + Some($get_item(self.caps, self.n_structures - 1).unwrap()) } } } @@ -754,7 +760,7 @@ macro_rules! define_iter( self.n_structures -= 1; unsafe { - Some($get_item(self.caps, self.n_structures as u32).unwrap()) + Some($get_item(self.caps, self.n_structures).unwrap()) } } @@ -766,7 +772,7 @@ macro_rules! define_iter( } else { self.n_structures = end - 1; unsafe { - Some($get_item(self.caps, self.n_structures as u32).unwrap()) + Some($get_item(self.caps, self.n_structures).unwrap()) } } } @@ -783,7 +789,7 @@ define_iter!( &'a CapsRef, &'a StructureRef, |caps: &CapsRef, idx| { - let ptr = ffi::gst_caps_get_structure(caps.as_ptr(), idx); + let ptr = ffi::gst_caps_get_structure(caps.as_ptr(), idx as u32); if ptr.is_null() { None } else { @@ -798,7 +804,7 @@ define_iter!( &'a mut CapsRef, &'a mut StructureRef, |caps: &CapsRef, idx| { - let ptr = ffi::gst_caps_get_structure(caps.as_ptr(), idx); + let ptr = ffi::gst_caps_get_structure(caps.as_ptr(), idx as u32); if ptr.is_null() { None } else { @@ -811,8 +817,8 @@ define_iter!( &'a CapsRef, (&'a StructureRef, &'a CapsFeaturesRef), |caps: &CapsRef, idx| { - let ptr1 = ffi::gst_caps_get_structure(caps.as_ptr(), idx); - let ptr2 = ffi::gst_caps_get_features(caps.as_ptr(), idx); + let ptr1 = ffi::gst_caps_get_structure(caps.as_ptr(), idx as u32); + let ptr2 = ffi::gst_caps_get_features(caps.as_ptr(), idx as u32); if ptr1.is_null() || ptr2.is_null() { None } else { @@ -828,8 +834,8 @@ define_iter!( &'a mut CapsRef, (&'a mut StructureRef, &'a mut CapsFeaturesRef), |caps: &CapsRef, idx| { - let ptr1 = ffi::gst_caps_get_structure(caps.as_ptr(), idx); - let ptr2 = ffi::gst_caps_get_features(caps.as_ptr(), idx); + let ptr1 = ffi::gst_caps_get_structure(caps.as_ptr(), idx as u32); + let ptr2 = ffi::gst_caps_get_features(caps.as_ptr(), idx as u32); if ptr1.is_null() || ptr2.is_null() { None } else { diff --git a/gstreamer/src/caps_features.rs b/gstreamer/src/caps_features.rs index 773a17d27..41765588b 100644 --- a/gstreamer/src/caps_features.rs +++ b/gstreamer/src/caps_features.rs @@ -383,19 +383,19 @@ impl CapsFeaturesRef { #[doc(alias = "get_size")] #[doc(alias = "gst_caps_features_get_size")] - pub fn size(&self) -> u32 { - unsafe { ffi::gst_caps_features_get_size(self.as_ptr()) } + pub fn size(&self) -> usize { + unsafe { ffi::gst_caps_features_get_size(self.as_ptr()) as usize } } #[doc(alias = "get_nth")] #[doc(alias = "gst_caps_features_get_nth")] - pub fn nth(&self, idx: u32) -> Option<&glib::GStr> { + pub fn nth(&self, idx: usize) -> Option<&glib::GStr> { if idx >= self.size() { return None; } unsafe { - let feature = ffi::gst_caps_features_get_nth(self.as_ptr(), idx); + let feature = ffi::gst_caps_features_get_nth(self.as_ptr(), idx as u32); if feature.is_null() { return None; } @@ -405,13 +405,13 @@ impl CapsFeaturesRef { } #[doc(alias = "gst_caps_features_get_nth_id")] - pub fn nth_quark(&self, idx: u32) -> Option { + pub fn nth_quark(&self, idx: usize) -> Option { if idx >= self.size() { return None; } unsafe { - let feature = ffi::gst_caps_features_get_nth_id(self.as_ptr(), idx); + let feature = ffi::gst_caps_features_get_nth_id(self.as_ptr(), idx as u32); Some(from_glib(feature)) } } @@ -552,7 +552,7 @@ impl<'a> Iter<'a> { Iter { caps_features, idx: 0, - n_features: n_features as usize, + n_features, } } } diff --git a/gstreamer/src/structure.rs b/gstreamer/src/structure.rs index 416fa950c..bdaa5cc19 100644 --- a/gstreamer/src/structure.rs +++ b/gstreamer/src/structure.rs @@ -658,13 +658,13 @@ impl StructureRef { #[doc(alias = "get_nth_field_name")] #[doc(alias = "gst_structure_nth_field_name")] - pub fn nth_field_name<'a>(&self, idx: u32) -> Option<&'a glib::GStr> { + pub fn nth_field_name<'a>(&self, idx: usize) -> Option<&'a glib::GStr> { if idx >= self.n_fields() { return None; } unsafe { - let field_name = ffi::gst_structure_nth_field_name(&self.0, idx); + let field_name = ffi::gst_structure_nth_field_name(&self.0, idx as u32); debug_assert!(!field_name.is_null()); Some(glib::GStr::from_ptr(field_name)) @@ -672,8 +672,16 @@ impl StructureRef { } #[doc(alias = "gst_structure_n_fields")] - pub fn n_fields(&self) -> u32 { - unsafe { ffi::gst_structure_n_fields(&self.0) as u32 } + pub fn n_fields(&self) -> usize { + unsafe { ffi::gst_structure_n_fields(&self.0) as usize } + } + + pub fn len(&self) -> usize { + self.n_fields() + } + + pub fn is_empty(&self) -> bool { + self.n_fields() == 0 } #[doc(alias = "gst_structure_can_intersect")] @@ -1001,7 +1009,7 @@ impl<'a> FieldIterator<'a> { FieldIterator { structure, idx: 0, - n_fields: n_fields as usize, + n_fields, } } } @@ -1014,7 +1022,7 @@ impl<'a> Iterator for FieldIterator<'a> { return None; } - let field_name = self.structure.nth_field_name(self.idx as u32).unwrap(); + let field_name = self.structure.nth_field_name(self.idx).unwrap(); self.idx += 1; Some(field_name) @@ -1034,7 +1042,7 @@ impl<'a> DoubleEndedIterator for FieldIterator<'a> { } self.n_fields -= 1; - Some(self.structure.nth_field_name(self.n_fields as u32).unwrap()) + Some(self.structure.nth_field_name(self.n_fields).unwrap()) } } diff --git a/gstreamer/src/tags.rs b/gstreamer/src/tags.rs index deb9e3e3e..9e84e6b59 100644 --- a/gstreamer/src/tags.rs +++ b/gstreamer/src/tags.rs @@ -464,18 +464,18 @@ impl TagListRef { } #[doc(alias = "gst_tag_list_n_tags")] - pub fn n_tags(&self) -> u32 { - unsafe { ffi::gst_tag_list_n_tags(self.as_ptr()) as u32 } + pub fn n_tags(&self) -> usize { + unsafe { ffi::gst_tag_list_n_tags(self.as_ptr()) as usize } } #[doc(alias = "gst_tag_list_nth_tag_name")] - pub fn nth_tag_name(&self, idx: u32) -> Option<&glib::GStr> { + pub fn nth_tag_name(&self, idx: usize) -> Option<&glib::GStr> { if idx >= self.n_tags() { return None; } unsafe { - let name = ffi::gst_tag_list_nth_tag_name(self.as_ptr(), idx); + let name = ffi::gst_tag_list_nth_tag_name(self.as_ptr(), idx as u32); debug_assert!(!name.is_null()); Some(glib::GStr::from_ptr(name)) } @@ -483,7 +483,7 @@ impl TagListRef { #[doc(alias = "get_index")] #[doc(alias = "gst_tag_list_get_index")] - pub fn index<'a, T: Tag<'a>>(&self, idx: u32) -> Option<&'a TagValue> { + pub fn index<'a, T: Tag<'a>>(&self, idx: usize) -> Option<&'a TagValue> { self.index_generic(T::TAG_NAME, idx).map(|value| { if !value.is::() { panic!( @@ -498,8 +498,9 @@ impl TagListRef { #[doc(alias = "get_index_generic")] #[doc(alias = "gst_tag_list_get_index")] - pub fn index_generic(&self, tag_name: impl IntoGStr, idx: u32) -> Option<&SendValue> { + pub fn index_generic(&self, tag_name: impl IntoGStr, idx: usize) -> Option<&SendValue> { unsafe { + let idx = u32::try_from(idx).ok()?; let value = tag_name.run_with_gstr(|tag_name| { ffi::gst_tag_list_get_value_index(self.as_ptr(), tag_name.as_ptr(), idx) }); @@ -514,16 +515,16 @@ impl TagListRef { #[doc(alias = "get_size")] #[doc(alias = "gst_tag_list_get_tag_size")] - pub fn size<'a, T: Tag<'a>>(&self) -> u32 { + pub fn size<'a, T: Tag<'a>>(&self) -> usize { self.size_by_name(T::TAG_NAME) } #[doc(alias = "get_size_by_name")] #[doc(alias = "gst_tag_list_get_tag_size")] - pub fn size_by_name(&self, tag_name: impl IntoGStr) -> u32 { + pub fn size_by_name(&self, tag_name: impl IntoGStr) -> usize { unsafe { tag_name.run_with_gstr(|tag_name| { - ffi::gst_tag_list_get_tag_size(self.as_ptr(), tag_name.as_ptr()) + ffi::gst_tag_list_get_tag_size(self.as_ptr(), tag_name.as_ptr()) as usize }) } } @@ -648,7 +649,7 @@ impl<'a, T: Tag<'a>> TagIter<'a, T> { TagIter { taglist, idx: 0, - size: taglist.size::() as usize, + size: taglist.size::(), phantom: PhantomData, } } @@ -666,7 +667,7 @@ where return None; } - let item = self.taglist.index::(self.idx as u32).unwrap(); + let item = self.taglist.index::(self.idx).unwrap(); self.idx += 1; Some(item) @@ -689,7 +690,7 @@ where None } else { self.idx = end + 1; - Some(self.taglist.index::(end as u32).unwrap()) + Some(self.taglist.index::(end).unwrap()) } } @@ -697,7 +698,7 @@ where if self.idx == self.size { None } else { - Some(self.taglist.index::(self.size as u32 - 1).unwrap()) + Some(self.taglist.index::(self.size - 1).unwrap()) } } } @@ -713,7 +714,7 @@ where } self.size -= 1; - Some(self.taglist.index::(self.size as u32).unwrap()) + Some(self.taglist.index::(self.size).unwrap()) } fn nth_back(&mut self, n: usize) -> Option { @@ -723,7 +724,7 @@ where None } else { self.size = end - 1; - Some(self.taglist.index::(self.size as u32).unwrap()) + Some(self.taglist.index::(self.size).unwrap()) } } } @@ -757,7 +758,7 @@ impl<'a> GenericTagIter<'a> { taglist, name, idx: 0, - size: taglist.size_by_name(name) as usize, + size: taglist.size_by_name(name), } } } @@ -770,10 +771,7 @@ impl<'a> Iterator for GenericTagIter<'a> { return None; } - let item = self - .taglist - .index_generic(self.name, self.idx as u32) - .unwrap(); + let item = self.taglist.index_generic(self.name, self.idx).unwrap(); self.idx += 1; Some(item) @@ -796,7 +794,7 @@ impl<'a> Iterator for GenericTagIter<'a> { None } else { self.idx = end + 1; - Some(self.taglist.index_generic(self.name, end as u32).unwrap()) + Some(self.taglist.index_generic(self.name, end).unwrap()) } } @@ -806,7 +804,7 @@ impl<'a> Iterator for GenericTagIter<'a> { } else { Some( self.taglist - .index_generic(self.name, self.size as u32 - 1) + .index_generic(self.name, self.size - 1) .unwrap(), ) } @@ -820,11 +818,7 @@ impl<'a> DoubleEndedIterator for GenericTagIter<'a> { } self.size -= 1; - Some( - self.taglist - .index_generic(self.name, self.size as u32) - .unwrap(), - ) + Some(self.taglist.index_generic(self.name, self.size).unwrap()) } fn nth_back(&mut self, n: usize) -> Option { @@ -834,11 +828,7 @@ impl<'a> DoubleEndedIterator for GenericTagIter<'a> { None } else { self.size = end - 1; - Some( - self.taglist - .index_generic(self.name, self.size as u32) - .unwrap(), - ) + Some(self.taglist.index_generic(self.name, self.size).unwrap()) } } } @@ -861,7 +851,7 @@ impl<'a> GenericIter<'a> { GenericIter { taglist, idx: 0, - size: if size > 0 { size as usize } else { 0 }, + size: if size > 0 { size } else { 0 }, } } } @@ -874,7 +864,7 @@ impl<'a> Iterator for GenericIter<'a> { return None; } - let name = self.taglist.nth_tag_name(self.idx as u32).unwrap(); + let name = self.taglist.nth_tag_name(self.idx).unwrap(); let item = (name, self.taglist.iter_tag_generic(name)); self.idx += 1; @@ -898,7 +888,7 @@ impl<'a> Iterator for GenericIter<'a> { None } else { self.idx = end + 1; - let name = self.taglist.nth_tag_name(end as u32).unwrap(); + let name = self.taglist.nth_tag_name(end).unwrap(); Some((name, self.taglist.iter_tag_generic(name))) } } @@ -907,7 +897,7 @@ impl<'a> Iterator for GenericIter<'a> { if self.idx == self.size { None } else { - let name = self.taglist.nth_tag_name(self.size as u32 - 1).unwrap(); + let name = self.taglist.nth_tag_name(self.size - 1).unwrap(); Some((name, self.taglist.iter_tag_generic(name))) } } @@ -920,7 +910,7 @@ impl<'a> DoubleEndedIterator for GenericIter<'a> { } self.size -= 1; - let name = self.taglist.nth_tag_name(self.idx as u32).unwrap(); + let name = self.taglist.nth_tag_name(self.idx).unwrap(); Some((name, self.taglist.iter_tag_generic(name))) } @@ -931,7 +921,7 @@ impl<'a> DoubleEndedIterator for GenericIter<'a> { None } else { self.size = end - 1; - let name = self.taglist.nth_tag_name(self.size as u32).unwrap(); + let name = self.taglist.nth_tag_name(self.size).unwrap(); Some((name, self.taglist.iter_tag_generic(name))) } } @@ -955,7 +945,7 @@ impl<'a> Iter<'a> { Iter { taglist, idx: 0, - size: if size > 0 { size as usize } else { 0 }, + size: if size > 0 { size } else { 0 }, } } } @@ -968,7 +958,7 @@ impl<'a> Iterator for Iter<'a> { return None; } - let name = self.taglist.nth_tag_name(self.idx as u32).unwrap(); + let name = self.taglist.nth_tag_name(self.idx).unwrap(); let item = (name, self.taglist.generic(name).unwrap()); self.idx += 1; @@ -992,7 +982,7 @@ impl<'a> Iterator for Iter<'a> { None } else { self.idx = end + 1; - let name = self.taglist.nth_tag_name(end as u32).unwrap(); + let name = self.taglist.nth_tag_name(end).unwrap(); Some((name, self.taglist.generic(name).unwrap())) } } @@ -1001,7 +991,7 @@ impl<'a> Iterator for Iter<'a> { if self.idx == self.size { None } else { - let name = self.taglist.nth_tag_name(self.size as u32 - 1).unwrap(); + let name = self.taglist.nth_tag_name(self.size - 1).unwrap(); Some((name, self.taglist.generic(name).unwrap())) } } @@ -1014,7 +1004,7 @@ impl<'a> DoubleEndedIterator for Iter<'a> { } self.size -= 1; - let name = self.taglist.nth_tag_name(self.idx as u32).unwrap(); + let name = self.taglist.nth_tag_name(self.idx).unwrap(); Some((name, self.taglist.generic(name).unwrap())) } @@ -1025,7 +1015,7 @@ impl<'a> DoubleEndedIterator for Iter<'a> { None } else { self.size = end - 1; - let name = self.taglist.nth_tag_name(self.size as u32).unwrap(); + let name = self.taglist.nth_tag_name(self.size).unwrap(); Some((name, self.taglist.generic(name).unwrap())) } } diff --git a/gstreamer/src/tags_serde.rs b/gstreamer/src/tags_serde.rs index 49d53466d..dff6ec3aa 100644 --- a/gstreamer/src/tags_serde.rs +++ b/gstreamer/src/tags_serde.rs @@ -117,7 +117,7 @@ impl<'a> Serialize for TagListSer<'a> { let tag_count = self.0.n_tags(); match tag_count.cmp(&0) { cmp::Ordering::Greater => { - let mut seq = serializer.serialize_seq(Some(tag_count as usize))?; + let mut seq = serializer.serialize_seq(Some(tag_count))?; let tag_list_iter = self.0.iter_generic(); for (tag_name, tag_iter) in tag_list_iter { seq.serialize_element(&TagsSer::new(tag_name, tag_iter))?;