Skip to content
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
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"bd-api",
"bd-artifact-upload",
"bd-bonjson",
"bd-bonjson-ffi",
"bd-bonjson-tests",
"bd-bounded-buffer",
"bd-buffer",
Expand Down
25 changes: 25 additions & 0 deletions bd-bonjson-ffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
edition = "2024"
license-file = "../LICENSE"
name = "bd-bonjson-ffi"
publish = false
version = "1.0.0"

[lib]
doctest = false

[dev-dependencies]
assert_no_alloc.workspace = true
bd-test-helpers.path = "../bd-test-helpers"
ctor.workspace = true
tempfile.workspace = true

[build-dependencies]
cbindgen.workspace = true

[dependencies]
bd-bonjson.path = "../bd-bonjson"
bd-workspace-hack.workspace = true
bytes.workspace = true
libc.workspace = true
log.workspace = true
2 changes: 1 addition & 1 deletion bd-bonjson/build.rs → bd-bonjson-ffi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub fn main() {
return;
}

println!("cargo:rerun-if-changed=src/ffi.rs");
println!("cargo:rerun-if-changed=src/lib.rs");
cbindgen::Builder::new()
.with_crate(Path::new("."))
.with_config(Config::from_file(Path::new("cbindgen.toml")).unwrap())
Expand Down
File renamed without changes.
45 changes: 45 additions & 0 deletions bd-bonjson-ffi/include/bd-bonjson/ffi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// shared-core - bitdrift's common client/server libraries
// Copyright Bitdrift, Inc. All rights reserved.
//
// Use of this source code is governed by a source available license that can be found in the
// LICENSE file or at:
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt

#pragma once

/* Warning: this file is autogenerated by cbindgen. Don't modify this manually. */

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef const void **BDCrashWriterHandle;

/**
* Attempt to open a writer to the specified path.
* The writer exists on the heap and must be closed with `bdcrw_close_writer`.
*/
bool bdcrw_open_writer(BDCrashWriterHandle handle, const char *path);

void bdcrw_close_writer(BDCrashWriterHandle handle);

bool bdcrw_flush_writer(BDCrashWriterHandle handle);

bool bdcrw_write_boolean(BDCrashWriterHandle handle, bool value);

bool bdcrw_write_null(BDCrashWriterHandle handle);

bool bdcrw_write_signed(BDCrashWriterHandle handle, int64_t value);

bool bdcrw_write_unsigned(BDCrashWriterHandle handle, uint64_t value);

bool bdcrw_write_float(BDCrashWriterHandle handle, double value);

bool bdcrw_write_str(BDCrashWriterHandle handle, const char *value);

bool bdcrw_write_array_begin(BDCrashWriterHandle handle);

bool bdcrw_write_map_begin(BDCrashWriterHandle handle);

bool bdcrw_write_container_end(BDCrashWriterHandle handle);
39 changes: 34 additions & 5 deletions bd-bonjson/src/ffi.rs → bd-bonjson-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,45 @@
// LICENSE file or at:
// https://polyformproject.org/wp-content/uploads/2020/06/PolyForm-Shield-1.0.0.txt

use crate::writer::Writer;
use bd_bonjson::writer::Writer;
use std::ffi::c_void;
use std::fs::{File, OpenOptions};
use std::io::{self, BufWriter, Write};
use std::ops::{Deref, DerefMut};
use std::ptr::null;

pub type BDCrashWriterHandle = *mut *const c_void;
pub type WriterBufWriterFile = Writer<BufWriter<File>>;

/// Newtype wrapper around `Writer<BufWriter<File>>` to allow implementing traits
pub struct WriterBufWriterFile(Writer<BufWriter<File>>);

impl WriterBufWriterFile {
#[must_use]
pub fn new(writer: Writer<BufWriter<File>>) -> Self {
Self(writer)
}

#[must_use]
pub fn into_raw(self) -> *const c_void {
Box::into_raw(Box::new(self)) as *const c_void
}
}

impl Deref for WriterBufWriterFile {
type Target = Writer<BufWriter<File>>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl DerefMut for WriterBufWriterFile {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}



#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FFIError {
Expand Down Expand Up @@ -58,9 +89,7 @@ fn new_writer(path: &str) -> io::Result<WriterBufWriterFile> {
.write(true)
.truncate(true)
.open(path)?;
Ok(Writer {
writer: BufWriter::new(file),
})
Ok(WriterBufWriterFile::new(Writer::new(BufWriter::new(file))))
}

/// Attempt to open a writer to the specified path.
Expand Down
3 changes: 2 additions & 1 deletion bd-bonjson-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ doctest = false

[dependencies]
bd-bonjson.path = "../bd-bonjson"
bd-bonjson-ffi.path = "../bd-bonjson-ffi"
bd-workspace-hack.workspace = true
libc.workspace = true

Expand All @@ -19,5 +20,5 @@ bd-test-helpers.path = "../bd-test-helpers"
tempfile.workspace = true

[build-dependencies]
bd-bonjson.path = "../bd-bonjson"
bd-bonjson-ffi.path = "../bd-bonjson-ffi"
cc.workspace = true
4 changes: 2 additions & 2 deletions bd-bonjson-tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

pub fn main() {
println!("cargo:rerun-if-changed=src/ffi_tests.c");
println!("cargo:rerun-if-changed=../bd-bonjson/src/ffi.rs");
println!("cargo:rerun-if-changed=../bd-bonjson-ffi/src/lib.rs");
cc::Build::new()
.cpp(false)
.file("src/ffi_tests.c")
.include("../bd-bonjson/include")
.include("../bd-bonjson-ffi/include")
.compile("bdbj");
}
2 changes: 1 addition & 1 deletion bd-bonjson-tests/src/ffi_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use assert_no_alloc::*;
use bd_bonjson::Value;
use bd_bonjson::ffi::BDCrashWriterHandle;
use bd_bonjson_ffi::BDCrashWriterHandle;
use std::ptr::null;
use tempfile::NamedTempFile;

Expand Down
1 change: 0 additions & 1 deletion bd-bonjson/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

pub mod decoder;
pub mod encoder;
pub mod ffi;
pub mod writer;

mod deserialize_primitives;
Expand Down
5 changes: 5 additions & 0 deletions bd-bonjson/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ pub struct Writer<W: std::io::Write + Send + Sync> {
}

impl<W: std::io::Write + Send + Sync> Writer<W> {
/// Create a new Writer with the given underlying writer
pub fn new(writer: W) -> Self {
Self { writer }
}

#[must_use]
pub fn into_raw(self) -> *const c_void {
Box::into_raw(Box::new(self)) as *const _
Expand Down
2 changes: 1 addition & 1 deletion bd-bonjson/src/writer_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn writer_does_not_allocate_writing_to_file() {

// Use BufWriter to avoid frequent system calls
let buf_writer = Box::new(BufWriter::new(file));
let mut writer = Writer { writer: buf_writer };
let mut writer = Writer::new(buf_writer);

assert_no_alloc(|| {
for _ in 0 .. 100_000 {
Expand Down
Loading