gstreamer: format: Implement some more conversion traits

Specifically, `From<$formatted_type> for $inner` and
`TryFrom<$formatted_type> for usize` for some types.

Fixes https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/issues/492

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer-rs/-/merge_requests/1329>
This commit is contained in:
Sebastian Dröge 2023-10-25 12:09:45 +03:00
parent 414019af21
commit 4d19d7b0b6
3 changed files with 38 additions and 0 deletions

View file

@ -74,6 +74,14 @@ impl TryFromGlib<i64> for Other {
} }
} }
impl TryFrom<Other> for usize {
type Error = std::num::TryFromIntError;
fn try_from(value: Other) -> Result<Self, Self::Error> {
value.0.try_into()
}
}
// FIXME `functions in traits cannot be const` (rustc 1.64.0) // FIXME `functions in traits cannot be const` (rustc 1.64.0)
// rustdoc-stripper-ignore-next // rustdoc-stripper-ignore-next
/// `Other` formatted value constructor trait. /// `Other` formatted value constructor trait.

View file

@ -281,6 +281,12 @@ macro_rules! impl_common_ops_for_newtype_uint(
} }
} }
impl From<$typ> for $inner {
fn from(v: $typ) -> $inner {
v.0
}
}
impl_trait_op_same!($typ, Add, add, AddAssign, add_assign); impl_trait_op_same!($typ, Add, add, AddAssign, add_assign);
impl_trait_op_same!($typ, Sub, sub, SubAssign, sub_assign); impl_trait_op_same!($typ, Sub, sub, SubAssign, sub_assign);
impl std::ops::Div for $typ { impl std::ops::Div for $typ {

View file

@ -67,6 +67,14 @@ impl_format_value_traits!(Buffers, Buffers, Buffers, u64);
option_glib_newtype_from_to!(Buffers, Buffers::OFFSET_NONE); option_glib_newtype_from_to!(Buffers, Buffers::OFFSET_NONE);
glib_newtype_display!(Buffers, DisplayableOptionBuffers, Format::Buffers); glib_newtype_display!(Buffers, DisplayableOptionBuffers, Format::Buffers);
impl TryFrom<Buffers> for usize {
type Error = std::num::TryFromIntError;
fn try_from(value: Buffers) -> Result<Self, Self::Error> {
value.0.try_into()
}
}
// FIXME `functions in traits cannot be const` (rustc 1.64.0) // FIXME `functions in traits cannot be const` (rustc 1.64.0)
// rustdoc-stripper-ignore-next // rustdoc-stripper-ignore-next
/// `Buffers` formatted value constructor trait. /// `Buffers` formatted value constructor trait.
@ -143,6 +151,14 @@ impl_format_value_traits!(Bytes, Bytes, Bytes, u64);
option_glib_newtype_from_to!(Bytes, u64::MAX); option_glib_newtype_from_to!(Bytes, u64::MAX);
glib_newtype_display!(Bytes, DisplayableOptionBytes, Format::Bytes); glib_newtype_display!(Bytes, DisplayableOptionBytes, Format::Bytes);
impl TryFrom<Bytes> for usize {
type Error = std::num::TryFromIntError;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
value.0.try_into()
}
}
// FIXME `functions in traits cannot be const` (rustc 1.64.0) // FIXME `functions in traits cannot be const` (rustc 1.64.0)
// rustdoc-stripper-ignore-next // rustdoc-stripper-ignore-next
/// `Bytes` formatted value constructor trait. /// `Bytes` formatted value constructor trait.
@ -240,6 +256,14 @@ impl_format_value_traits!(Default, Default, Default, u64);
option_glib_newtype_from_to!(Default, u64::MAX); option_glib_newtype_from_to!(Default, u64::MAX);
glib_newtype_display!(Default, DisplayableOptionDefault, Format::Default); glib_newtype_display!(Default, DisplayableOptionDefault, Format::Default);
impl TryFrom<Default> for usize {
type Error = std::num::TryFromIntError;
fn try_from(value: Default) -> Result<Self, Self::Error> {
value.0.try_into()
}
}
// FIXME `functions in traits cannot be const` (rustc 1.64.0) // FIXME `functions in traits cannot be const` (rustc 1.64.0)
// rustdoc-stripper-ignore-next // rustdoc-stripper-ignore-next
/// `Default` formatted value constructor trait. /// `Default` formatted value constructor trait.