Skip to content

Commit

Permalink
module: make write_bitcode_to_path accept more general path represent…
Browse files Browse the repository at this point in the history
…ations (#528)

Co-authored-by: Dan Kolsoi <[email protected]>
  • Loading branch information
airwoodix and TheDan64 committed Aug 22, 2024
1 parent a4e37ac commit 4028abc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
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

0 comments on commit 4028abc

Please sign in to comment.