Hello.
I am trying to compile with rust library in c++ code (main.cpp
) with cxx.rs
I have a problem with it. File lib.rs.h
not generate.
// src/lib.rs
use cxx::CxxString;
use std::io::{self, Write};
use std::pin::Pin;
#[cxx::bridge]
mod ffi {
extern "Rust" {
fn prettify_json(
input: &[u8],
output: Pin<&mut CxxString>) -> Result<()>;
}
}
struct WriteToCxxString<'a>(Pin<&'a mut CxxString>);
impl<'a> Write for WriteToCxxString<'a> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.as_mut().push_bytes(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
fn prettify_json(input: &[u8],
output: Pin<&mut CxxString>) -> serde_json::Result<()>
{
let writer = WriteToCxxString(output);
let mut deserializer = serde_json::Deserializer::from_slice(input);
let mut serializer = serde_json::Serializer::pretty(writer);
serde_transcode::transcode(&mut deserializer, &mut serializer)
}
// src/main.cpp
#include "lib.rs.h"
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
int main()
{
// Read json from stdin.
std::istreambuf_iterator<char> begin{std::cin}, end;
std::vector<unsigned char> input{begin, end};
rust::Slice<const uint8_t> slice{input.data(), input.size()};
// Prettify using serde_json and serde_transcode.
std::string output;
prettify_json(slice, output);
// Write to stdout.
std::cout << output << std::endl;
}
Cargo.toml:
[package]
name = "cxx-rust"
edition = "2018"
version = "0.1.0"
authors = ["Michał Hanusek <mhanusek@o2.pl>"]
[dependencies]
cxx = "*"
serde = { version = "1", features = ["derive"] }
serde_json = "*"
serde-transcode = "*"
[lib]
crate-type = ["staticlib"]
Repo with code: