Skip to content

Commit eb5d0bc

Browse files
committed
add impl From<T> for Value for manually implemented types
1 parent d9851e3 commit eb5d0bc

23 files changed

+371
-15
lines changed

cairo/src/context.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ impl fmt::Display for RectangleList {
7171
#[repr(transparent)]
7272
pub struct Context(ptr::NonNull<cairo_t>);
7373

74+
#[cfg(feature = "use_glib")]
75+
impl IntoGlibPtr<*mut ffi::cairo_t> for Context {
76+
#[inline]
77+
unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_t {
78+
(&*std::mem::ManuallyDrop::new(self)).to_glib_none().0
79+
}
80+
}
81+
7482
#[cfg(feature = "use_glib")]
7583
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_t> for &'a Context {
7684
type Storage = &'a Context;

cairo/src/device.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,14 @@ impl Device {
311311
}
312312
}
313313

314+
#[cfg(feature = "use_glib")]
315+
impl IntoGlibPtr<*mut ffi::cairo_device_t> for Device {
316+
#[inline]
317+
unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_device_t {
318+
std::mem::ManuallyDrop::new(self).to_glib_none().0
319+
}
320+
}
321+
314322
#[cfg(feature = "use_glib")]
315323
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_device_t> for Device {
316324
type Storage = &'a Device;

cairo/src/enums.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ macro_rules! gvalue_impl {
4242
<Self as glib::StaticType>::static_type()
4343
}
4444
}
45+
46+
impl From<$name> for glib::Value {
47+
#[inline]
48+
fn from(v: $name) -> Self {
49+
glib::value::ToValue::to_value(&v)
50+
}
51+
}
4552
};
4653
}
4754

cairo/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ macro_rules! gvalue_impl {
8686
}
8787
}
8888

89+
impl From<$name> for glib::Value {
90+
fn from(v: $name) -> Self {
91+
unsafe {
92+
let mut value =
93+
glib::Value::from_type(<$name as glib::StaticType>::static_type());
94+
glib::gobject_ffi::g_value_take_boxed(
95+
value.to_glib_none_mut().0,
96+
glib::translate::IntoGlibPtr::into_glib_ptr(v) as *mut _,
97+
);
98+
value
99+
}
100+
}
101+
}
102+
89103
impl glib::value::ToValueOptional for $name {
90104
fn to_value_optional(s: Option<&Self>) -> glib::Value {
91105
let mut value = glib::Value::for_value_type::<Self>();

cairo/src/region.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ use crate::ffi::cairo_region_t;
1515
#[repr(transparent)]
1616
pub struct Region(ptr::NonNull<cairo_region_t>);
1717

18+
#[cfg(feature = "use_glib")]
19+
impl IntoGlibPtr<*mut ffi::cairo_region_t> for Region {
20+
#[inline]
21+
unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_region_t {
22+
(&*std::mem::ManuallyDrop::new(self)).to_glib_none().0
23+
}
24+
}
25+
1826
#[cfg(feature = "use_glib")]
1927
#[doc(hidden)]
2028
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_region_t> for &'a Region {

cairo/src/surface.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,14 @@ impl Surface {
289289
}
290290
}
291291

292+
#[cfg(feature = "use_glib")]
293+
impl IntoGlibPtr<*mut ffi::cairo_surface_t> for Surface {
294+
#[inline]
295+
unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_surface_t {
296+
std::mem::ManuallyDrop::new(self).to_glib_none().0
297+
}
298+
}
299+
292300
#[cfg(feature = "use_glib")]
293301
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_surface_t> for Surface {
294302
type Storage = &'a Surface;

cairo/src/surface_macros.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ macro_rules! declare_surface {
3535
}
3636
}
3737

38+
#[cfg(feature = "use_glib")]
39+
impl IntoGlibPtr<*mut ffi::cairo_surface_t> for $surf_name {
40+
#[inline]
41+
unsafe fn into_glib_ptr(self) -> *mut ffi::cairo_surface_t {
42+
std::mem::ManuallyDrop::new(self).to_glib_none().0
43+
}
44+
}
45+
3846
#[cfg(feature = "use_glib")]
3947
impl<'a> ToGlibPtr<'a, *mut ffi::cairo_surface_t> for $surf_name {
4048
type Storage = &'a Surface;

glib-macros/src/boxed_derive.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
161161
}
162162
}
163163

164+
impl ::std::convert::From<#name> for #crate_ident::Value {
165+
fn from(v: #name) -> Self {
166+
unsafe {
167+
let mut value = #crate_ident::Value::from_type(<#name as #crate_ident::StaticType>::static_type());
168+
#crate_ident::gobject_ffi::g_value_take_boxed(
169+
#crate_ident::translate::ToGlibPtrMut::to_glib_none_mut(&mut value).0,
170+
#crate_ident::translate::IntoGlibPtr::<*mut #name>::into_glib_ptr(v) as *mut _,
171+
);
172+
value
173+
}
174+
}
175+
}
176+
164177
#impl_to_value_optional
165178

166179
#impl_from_value
@@ -188,6 +201,13 @@ pub fn impl_boxed(input: &syn::DeriveInput) -> TokenStream {
188201
}
189202
}
190203

204+
impl #crate_ident::translate::IntoGlibPtr<*mut #name> for #name {
205+
#[inline]
206+
unsafe fn into_glib_ptr(self) -> *mut #name {
207+
::std::boxed::Box::into_raw(::std::boxed::Box::new(self)) as *mut _
208+
}
209+
}
210+
191211
impl<'a> #crate_ident::translate::ToGlibPtr<'a, *const #name> for #name {
192212
type Storage = &'a Self;
193213

glib-macros/src/enum_derive.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ pub fn impl_enum(input: &syn::DeriveInput) -> TokenStream {
146146
}
147147
}
148148

149+
impl ::std::convert::From<#name> for #crate_ident::Value {
150+
#[inline]
151+
fn from(v: #name) -> Self {
152+
#crate_ident::value::ToValue::to_value(&v)
153+
}
154+
}
155+
149156
impl #crate_ident::StaticType for #name {
150157
fn static_type() -> #crate_ident::Type {
151158
static ONCE: ::std::sync::Once = ::std::sync::Once::new();

glib-macros/src/flags_attribute.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,13 @@ pub fn impl_flags(attrs: &NestedMeta, input: &DeriveInput) -> TokenStream {
174174
}
175175
}
176176

177+
impl ::std::convert::From<#name> for #crate_ident::Value {
178+
#[inline]
179+
fn from(v: #name) -> Self {
180+
#crate_ident::value::ToValue::to_value(&v)
181+
}
182+
}
183+
177184
impl #crate_ident::StaticType for #name {
178185
fn static_type() -> #crate_ident::Type {
179186
static ONCE: ::std::sync::Once = ::std::sync::Once::new();

0 commit comments

Comments
 (0)