Created
December 26, 2023 22:24
-
-
Save LorenDB/3963fec51ea76535e351dde213bfa05f to your computer and use it in GitHub Desktop.
An example Polyglot project that demonstrates how Rust can use C++ functions that use std::string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
std::string cppStdString() | |
{ | |
return "This is a C++ std::string"; | |
} | |
std::string lotsOfStdStrings(std::string a, std::string b) | |
{ | |
return a + b; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// While polybuild is not explicitly told to use this file, it is assumed that you will provide | |
// headers for any C++ code that you want exposed. After all, your other C++ code wouldn't be | |
// able to use it otherwise. | |
#include <string> | |
std::string cppStdString(); | |
std::string lotsOfStdStrings(std::string a, std::string b); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The content below was generated by Polyglot to wrap each std::string function to | |
// take and return C-style strings instead. | |
// *** WARNING: autogenerated file, do not modify. Changes will be overwritten. *** | |
// Generated by Polyglot version 0.0.1-devel at Tue Dec 26 17:18:00 2023. | |
// This file contains type proxies for C++. | |
#include <cstring> | |
#include "../../example.h" | |
extern "C" const char *cppStdString_polyglot_typeproxy() | |
{ | |
return strdup(cppStdString().c_str()); | |
} | |
extern "C" const char *lotsOfStdStrings_polyglot_typeproxy(const char *a, const char *b) | |
{ | |
return strdup(lotsOfStdStrings(a, b).c_str()); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The content below was generated by Polyglot. It uses Rust's FFI library to convert | |
// C-style strings into usable String objects. | |
// *** WARNING: autogenerated file, do not modify. Changes will be overwritten. *** | |
// Generated by Polyglot version 0.0.1-devel at Tue Dec 26 17:18:00 2023. | |
// This file contains symbols that have been exported from C++ into Rust. | |
// This import supports various Polyglot features; however, it may not be used in every | |
// Polyglot wrapper file. | |
use std::ffi::CString; | |
extern { | |
#[link_name = "cppStdString_polyglot_typeproxy"] fn cppStdString_polyglot_typeproxy() -> *mut i8; | |
} | |
#[allow(non_snake_case)] | |
pub fn cppStdString() -> String { | |
unsafe { | |
CString::from_raw(cppStdString_polyglot_typeproxy()) | |
.into_string() | |
.expect("Failed to convert C-style string to String in cppStdString") | |
} | |
} | |
extern { | |
#[link_name = "lotsOfStdStrings_polyglot_typeproxy"] fn lotsOfStdStrings_polyglot_typeproxy(a: *const i8, b: *const i8) -> *mut i8; | |
} | |
#[allow(non_snake_case)] | |
pub fn lotsOfStdStrings(a: String, b: String) -> String { | |
unsafe { | |
CString::from_raw(lotsOfStdStrings_polyglot_typeproxy( | |
CString::new(a).expect("Failed to convert parameter a of lotsOfStdStrings into CString").into_raw(), | |
CString::new(b).expect("Failed to convert parameter b of lotsOfStdStrings into CString").into_raw()) | |
) | |
.into_string() | |
.expect("Failed to convert C-style string to String in lotsOfStdStrings") | |
} | |
} | |
extern { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mod example; | |
fn main() { | |
println!("{}", example::cppStdString()); | |
println!("{}", example::lotsOfStdStrings("This is a ".to_string(), "two-part string".to_string())); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file tells polybuild which sources to use | |
name: example | |
sources: | |
- example.cpp | |
- main.rs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment