Skip to content

Instantly share code, notes, and snippets.

@shqld
Last active September 11, 2023 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shqld/7c29d3f83402a107435e9fd5cfe0376c to your computer and use it in GitHub Desktop.
Save shqld/7c29d3f83402a107435e9fd5cfe0376c to your computer and use it in GitHub Desktop.
struct Context {
_inner: *mut std::ffi::c_void,
}
impl Drop for Context {
fn drop(&mut self) {
println!("drop: Context")
}
}
impl Context {
fn new() -> Self {
Context {
_inner: std::ptr::null_mut(),
}
}
}
struct Module<'ctx> {
_inner: *mut std::ffi::c_void,
_context: &'ctx Context,
}
impl Drop for Module<'_> {
fn drop(&mut self) {
println!("drop: Module")
}
}
impl<'ctx> Module<'ctx> {
fn new(context: &'ctx Context) -> Self {
Module {
_inner: std::ptr::null_mut(),
_context: context,
}
}
fn add_global(&mut self, _ty: Type<'ctx>) {
unimplemented!()
// unsafe { LLVMAddGlobal(self._inner, ty._inner, to_cstring(name).as_ptr()) }
}
}
struct Type<'ctx> {
_inner: *mut std::ffi::c_void,
_context: &'ctx Context,
}
impl<'ctx> Type<'ctx> {
fn new(context: &'ctx Context) -> Self {
Type {
_inner: std::ptr::null_mut(),
_context: context,
}
}
}
fn main() {
let context = Context::new();
let mut module = Module::new(&context);
let context2 = Context::new();
module.add_global(Type::new(&context2));
}
use llvm_sys::{LLVMContext, LLVMModule, LLVMType};
use std::{cell::UnsafeCell, marker::PhantomData};
struct Context {
_inner: *mut LLVMContext,
}
impl Drop for Context {
fn drop(&mut self) {
println!("drop: Context")
}
}
impl Context {
fn new() -> Self {
Context {
_inner: std::ptr::null_mut(),
}
}
}
struct Module<'ctx> {
_inner: *mut LLVMModule,
_context: PhantomData<UnsafeCell<&'ctx Context>>,
}
impl Drop for Module<'_> {
fn drop(&mut self) {
println!("drop: Module")
}
}
impl<'ctx> Module<'ctx> {
fn new(context: &'ctx Context) -> Self {
Module {
_inner: std::ptr::null_mut(),
_context: PhantomData,
}
}
fn add_global(&self, _ty: Type<'ctx>) {
unimplemented!()
// unsafe { LLVMAddGlobal(self._inner, ty._inner, to_cstring(name).as_ptr()) }
}
}
struct Type<'ctx> {
_inner: *mut LLVMType,
_context: &'ctx Context,
}
impl<'ctx> Type<'ctx> {
fn new(context: &'ctx Context) -> Self {
Type {
_inner: std::ptr::null_mut(),
_context: context,
}
}
}
fn main() {
let context = Context::new();
let module = Module::new(&context);
let context2 = Context::new();
module.add_global(Type::new(&context2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment