Skip to content

Commit 8b9066a

Browse files
committed
2 parents b806565 + 9cb14a6 commit 8b9066a

File tree

14 files changed

+209
-79
lines changed

14 files changed

+209
-79
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
github: jhg

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ env:
2727

2828
jobs:
2929
test:
30+
needs: lint
3031
strategy:
3132
matrix:
3233
os: [ ubuntu-latest, macos-latest, windows-latest ]
3334
rust: [ stable, beta ]
3435
features: [ 'std', 'alloc', 'std,lender', 'alloc,lender' ]
3536
runs-on: ${{ matrix.os }}
3637
timeout-minutes: 5
38+
continue-on-error: true
3739
if: github.event_name != 'schedule' && github.event_name != 'workflow_dispatch' && !endsWith(github.ref, '-nightly') && !endsWith(github.head_ref, '-nightly')
3840
steps:
3941
- uses: actions/checkout@v2
@@ -67,13 +69,15 @@ jobs:
6769
- name: Run fmt
6870
run: cargo fmt --verbose --all -- --check
6971
nightly-test:
72+
needs: nightly-lint
7073
strategy:
7174
matrix:
7275
os: [ ubuntu-latest, macos-latest, windows-latest ]
7376
rust: [ nightly ]
7477
features: [ 'std', 'alloc', 'std,lender', 'alloc,lender' ]
7578
runs-on: ${{ matrix.os }}
7679
timeout-minutes: 5
80+
continue-on-error: true
7781
steps:
7882
- uses: actions/checkout@v2
7983
- uses: actions-rs/toolchain@v1

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/opaque-pointer-rs.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
[package]
22
name = "opaque-pointer"
3-
version = "0.8.8"
3+
version = "0.9.0"
44
description = "Generic functions to work with opaque pointers when use FFI to expose Rust structs"
5-
authors = ["Jesus Hernandez <[email protected]>"]
5+
authors = [
6+
"Jesus Hernandez <[email protected]>",
7+
"Dylan DPC",
8+
]
69
license = "Unlicense"
7-
keywords = ["pointer", "opaque", "pointers", "cbindgen", "ffi"]
10+
keywords = ["opaque", "pointers", "cbindgen", "ffi", "no_std"]
811
categories = ["development-tools::ffi", "no-std"]
912
edition = "2021"
1013
readme = "README.md"
1114
repository = "https://github.com/jhg/opaque-pointer-rs/"
12-
exclude = [".github/**", ".vscode/**"]
15+
exclude = [
16+
".github/**",
17+
".vscode/**",
18+
".idea/**",
19+
"tests/**", # To reduce the package size, as tests are not needed to use it.
20+
]
21+
rust-version = "1.57.0" # For HashSet::try_reserve
1322

1423
[features]
15-
default = ["std"]
24+
default = ["std", "lender"]
1625
std = []
17-
# If you does not use std then you need alloc feature
26+
lender = ["lazy_static"]
27+
# If you do not use std then you need alloc feature
1828
alloc = []
1929
# Allow to use some FFI for C types
2030
c-types = ["std"]
21-
lender = ["lazy_static"]
2231

2332
[dependencies]
2433
log = "0.4"
25-
lazy_static = { version = "^1", optional = true }
34+
lazy_static = { version = "1.4", optional = true }
2635

2736
[package.metadata.docs.rs]
2837
features = ["std", "lender"]

README.md

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
1-
# opaque-pointer-rs
1+
# Opaque Pointer for seamless Rust FFI
22

3-
Generic functions to work with opaque pointers when use FFI to expose Rust structs
3+
Harnessing generics with opaque pointers for seamless Rust FFI interoperability with C and C++
44

55
[![Crates.io](https://img.shields.io/crates/v/opaque-pointer)](https://crates.io/crates/opaque-pointer)
66
[![Crates.io](https://img.shields.io/crates/l/opaque-pointer)](https://unlicense.org/)
77
[![Crates.io](https://img.shields.io/crates/d/opaque-pointer)](https://crates.io/crates/opaque-pointer)
88

9-
## Basic usage
9+
## Overview
1010

11-
With this crate you can manage raw pointers easily to expose `structs` that will be
12-
use as opaque pointers from C or C++ calling to Rust functions to use it. This
13-
can be used with [cbindgen](https://crates.io/crates/cbindgen) crate with option `parse.parse_deps = true`
14-
for it will generate opaque C/C++ `structs` to use pointers in the arguments.
11+
Simplify raw pointer management to expose Rust structs as opaque pointers, seamlessly enabling interaction between Rust and C/C++ functions.
12+
This crate facilitates creating opaque C/C++ structs for use with arguments, generated by [cbindgen](https://crates.io/crates/cbindgen) with `parse.parse_deps = true`.
1513

16-
You can find more information about using Rust from other languages in
17-
[The Rust FFI Omnibus objects section](http://jakegoulding.com/rust-ffi-omnibus/objects/)
18-
of [Jake Goulding](https://github.com/shepmaster).
14+
For comprehensive insights into Rust's interoperability with other languages, explore [The Rust FFI Omnibus objects section](http://jakegoulding.com/rust-ffi-omnibus/objects/) by [Jake Goulding](https://github.com/shepmaster).
1915

2016
## Lender feature
2117

22-
If you activate the feature `lender` the functions like `own_back<T>()` will check if the pointer is valid.
23-
It means, the function result will be a error if the pointer was not returned by `raw<T>()`.
18+
By activating the `lender` feature, functions like `own_back<T>()` validate the pointer, ensuring its validity.
19+
It means, be returned by `raw<T>()`.
2420

2521
## Examples
2622

@@ -38,13 +34,14 @@ impl Counter {
3834
/// Ownership will NOT control the heap-allocated memory until own it back.
3935
#[no_mangle]
4036
pub extern fn counter_new(value: u8) -> *mut Counter {
41-
return opaque_pointer::raw(Counter::new());
37+
return opaque_pointer::raw(Counter::new())
38+
.expect("Error trying to lend a pointer");
4239
}
4340

4441
/// Drop (free memory of) Rust's Counter object as usually.
4542
#[no_mangle]
4643
pub extern fn counter_free(counter: *mut Counter) {
47-
unsafe { opaque_pointer::free(counter) };
44+
unsafe { opaque_pointer::own_back(counter) };
4845
}
4946

5047
#[no_mangle]

src/c.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use std::ffi::CStr;
88
use std::os::raw::c_char;
99

10-
use super::validate_pointer_is_not_null;
10+
use crate::validation::not_null_pointer;
1111

1212
use crate::error::PointerError;
1313

@@ -25,7 +25,7 @@ use crate::error::PointerError;
2525
pub unsafe fn ref_str<'a>(string: *const c_char) -> Result<&'a str, PointerError> {
2626
// ATTENTION! 'a lifetime is required, does NOT REMOVE it
2727
// see commit 5a03be91d2da8909986db7c54650f3a7863a91ff fixing 3a1d15f33e8e418ef6bee2b7b9e096780bd2c8ac
28-
validate_pointer_is_not_null(string)?;
28+
not_null_pointer(string)?;
2929
// CAUTION: this is unsafe
3030
let string = CStr::from_ptr(string);
3131
return Ok(string.to_str()?);

src/error.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Opaque pointers errors.
22
3+
#[cfg(all(feature = "alloc", not(feature = "std")))]
4+
use alloc::collections::TryReserveError;
35
use core::str::Utf8Error;
6+
#[cfg(feature = "std")]
7+
use std::collections::TryReserveError;
48

59
/// Errors that can be detected by the functions of this crate.
610
///
@@ -14,19 +18,29 @@ pub enum PointerError {
1418
Invalid,
1519
/// Trying to convert to `&str` a C string which content is not valid UTF-8.
1620
Utf8Error(Utf8Error),
21+
/// Trying to alloc memory, see [`alloc::collections::TryReserveError`].
22+
#[cfg(any(feature = "alloc", feature = "std"))]
23+
TryReserveError(TryReserveError),
1724
}
1825

1926
impl core::fmt::Display for PointerError {
2027
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
21-
match *self {
28+
match self {
2229
Self::Null => {
2330
write!(f, "dereference a null pointer will produce a crash")
2431
}
2532
Self::Invalid => {
2633
write!(f, "dereference a unknown pointer could produce a crash")
2734
}
28-
Self::Utf8Error(..) => {
29-
write!(f, "the provided C string is not a valid UTF-8 string")
35+
Self::Utf8Error(error) => {
36+
write!(
37+
f,
38+
"the provided C string is not a valid UTF-8 string: {error}"
39+
)
40+
}
41+
#[cfg(any(feature = "alloc", feature = "std"))]
42+
Self::TryReserveError(error) => {
43+
write!(f, "can not alloc memory of internal usage: {error}")
3044
}
3145
}
3246
}
@@ -35,15 +49,23 @@ impl core::fmt::Display for PointerError {
3549
#[cfg(feature = "std")] // Waiting for https://github.com/rust-lang/rust/issues/103765
3650
impl std::error::Error for PointerError {
3751
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
38-
match *self {
52+
match self {
3953
Self::Null | Self::Invalid => None,
40-
Self::Utf8Error(ref e) => Some(e),
54+
Self::Utf8Error(error) => Some(error),
55+
Self::TryReserveError(error) => Some(error),
4156
}
4257
}
4358
}
4459

4560
impl From<Utf8Error> for PointerError {
46-
fn from(err: Utf8Error) -> Self {
47-
Self::Utf8Error(err)
61+
fn from(error: Utf8Error) -> Self {
62+
Self::Utf8Error(error)
63+
}
64+
}
65+
66+
#[cfg(any(feature = "alloc", feature = "std"))]
67+
impl From<TryReserveError> for PointerError {
68+
fn from(error: TryReserveError) -> Self {
69+
Self::TryReserveError(error)
4870
}
4971
}

0 commit comments

Comments
 (0)