bitcoin-autofile

Crates.iobitcoin-autofile
lib.rsbitcoin-autofile
version0.1.20
created_at2025-11-30 20:55:29.24817+00
updated_at2025-11-30 20:55:29.24817+00
descriptionA non-refcounted RAII wrapper for `FILE*`. It is designed to manage the lifecycle of file pointers, ensuring that files are automatically closed when they go out of scope.
homepage
repositoryhttps://github.com/klebs6/bitcoin-rs
max_upload_size
id1958847
size191,996
(klebs6)

documentation

https://docs.rs/bitcoin-autofile

README

bitcoin-autofile

bitcoin-autofile is a Rust crate providing a non-refcounted RAII wrapper for FILE*. It is designed to manage the lifecycle of file pointers, ensuring that files are automatically closed when they go out of scope.

Overview

This crate defines a structure:

  • AutoFile: An RAII wrapper for FILE* that automatically closes the file when it goes out of scope.

AutoFile

The AutoFile structure manages a FILE* pointer, ensuring proper cleanup when the object is destroyed. It provides methods for file operations and ownership management.

Example Usage

use bitcoin_autofile::AutoFile;
use std::ptr;

let file_ptr: *mut libc::FILE = unsafe { libc::fopen(b"example.txt\0".as_ptr() as *const libc::c_char, b"w+\0".as_ptr() as *const libc::c_char) };
if file_ptr.is_null() {
    panic!("Failed to open file");
}

let mut autofile = AutoFile::new(file_ptr, 0, 0);
autofile.write(b"Hello, World!\0".as_ptr(), 13);

let file_ptr = autofile.release();  // Transfer ownership
unsafe {
    libc::fclose(file_ptr);
}

Features

  • RAII wrapper for FILE* to ensure automatic file closure.

  • Methods to release ownership or get the raw FILE* without releasing ownership.

  • Read, write, and ignore methods for file operations.

Commit count: 0

cargo fmt