Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: make write_bitcode_to_path accept more general path representations #528

Merged
merged 2 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,27 +648,30 @@ impl<'ctx> Module<'ctx> {
unsafe { GlobalValue::new(value) }
}

/// Writes a `Module` to a `Path`.
/// Writes a `Module` to a file.
///
/// # Arguments
///
/// * `path` - path to write the module's bitcode to. Must be valid Unicode.
///
/// # Example
///
/// ```no_run
/// use inkwell::context::Context;
///
/// use std::path::Path;
///
/// let mut path = Path::new("module.bc");
///
/// let context = Context::create();
/// let module = context.create_module("my_module");
/// let void_type = context.void_type();
/// let fn_type = void_type.fn_type(&[], false);
///
/// module.add_function("my_fn", fn_type, None);
/// module.write_bitcode_to_path(&path);
/// module.write_bitcode_to_path("module.bc");
/// ```
pub fn write_bitcode_to_path(&self, path: &Path) -> bool {
let path_str = path.to_str().expect("Did not find a valid Unicode path string");
pub fn write_bitcode_to_path(&self, path: impl AsRef<Path>) -> bool {
let path_str = path
.as_ref()
.to_str()
.expect("Did not find a valid Unicode path string");
let c_string = to_c_str(path_str);

unsafe { LLVMWriteBitcodeToFile(self.module.get(), c_string.as_ptr()) == 0 }
Expand Down
9 changes: 2 additions & 7 deletions tests/all/test_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use inkwell::values::AnyValue;
use inkwell::OptimizationLevel;

use std::env::temp_dir;
use std::fs::{remove_file, File};
use std::io::Read;
use std::fs::{self, remove_file};
use std::path::Path;

#[test]
Expand All @@ -24,11 +23,7 @@ fn test_write_bitcode_to_path() {
module.add_function("my_fn", fn_type, None);
module.write_bitcode_to_path(&path);

let mut contents = Vec::new();
let mut file = File::open(&path).expect("Could not open temp file");

file.read_to_end(&mut contents).expect("Unable to verify written file");

let contents = fs::read(&path).expect("Could not read back written file.");
assert!(!contents.is_empty());

remove_file(&path).unwrap();
Expand Down