1use context::Context;
2use std::marker::PhantomData;
3use std::fmt;
4use std::ffi::CStr;
5use std::str;
6
7use crate::context;
8
9#[derive(Copy, Clone)]
13pub struct Object<'ctx> {
14 marker: PhantomData<&'ctx Context<'ctx>>,
15 ptr: *mut gccjit_sys::gcc_jit_object
16}
17
18impl<'ctx> fmt::Debug for Object<'ctx> {
19 fn fmt<'a>(&self, fmt: &mut fmt::Formatter<'a>) -> Result<(), fmt::Error> {
20 unsafe {
21 let ptr = gccjit_sys::gcc_jit_object_get_debug_string(self.ptr);
22 let cstr = CStr::from_ptr(ptr);
23 let rust_str = str::from_utf8_unchecked(cstr.to_bytes());
24 fmt.write_str(rust_str)
25 }
26 }
27}
28
29use std::mem::ManuallyDrop;
30use std::ops::Deref;
31
32#[derive(Debug)]
33pub struct ContextRef<'ctx> {
34 context: ManuallyDrop<Context<'ctx>>,
35}
36
37impl<'ctx> Deref for ContextRef<'ctx> {
38 type Target = Context<'ctx>;
39
40 fn deref(&self) -> &Self::Target {
41 &self.context
42 }
43}
44
45impl<'ctx> Object<'ctx> {
46 pub fn get_context(&self) -> ContextRef<'ctx> {
47 unsafe {
48 ContextRef {
49 context: ManuallyDrop::new(context::from_ptr(gccjit_sys::gcc_jit_object_get_context(self.ptr))),
50 }
51 }
52 }
53}
54
55pub trait ToObject<'ctx> {
57 fn to_object(&self) -> Object<'ctx>;
58}
59
60impl<'ctx> ToObject<'ctx> for Object<'ctx> {
61 fn to_object(&self) -> Object<'ctx> {
62 unsafe { from_ptr(self.ptr) }
63 }
64}
65
66pub unsafe fn from_ptr<'ctx>(ptr: *mut gccjit_sys::gcc_jit_object) -> Object<'ctx> {
67 Object {
68 marker: PhantomData,
69 ptr
70 }
71}
72
73pub unsafe fn get_ptr<'ctx>(object: &Object<'ctx>) -> *mut gccjit_sys::gcc_jit_object {
74 object.ptr
75}
76
77