Skip to content

Commit 365fe4a

Browse files
committed
chore(pact_ffi): Add "C" ABI to all FFI functions
1 parent 9501b86 commit 365fe4a

6 files changed

Lines changed: 47 additions & 47 deletions

File tree

rust/pact_ffi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub extern "C" fn pactffi_version() -> *const c_char {
5858
///
5959
/// log_env_var must be a valid NULL terminated UTF-8 string.
6060
#[no_mangle]
61-
pub unsafe extern fn pactffi_init(log_env_var: *const c_char) {
61+
pub unsafe extern "C" fn pactffi_init(log_env_var: *const c_char) {
6262
let log_env_var = if !log_env_var.is_null() {
6363
let c_str = CStr::from_ptr(log_env_var);
6464
match c_str.to_str() {

rust/pact_ffi/src/mock_server/handles.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ impl MessageHandle {
428428
/// Returns a new `PactHandle`. The handle will need to be freed with the `pactffi_free_pact_handle`
429429
/// method to release its resources.
430430
#[no_mangle]
431-
pub extern fn pactffi_new_pact(consumer_name: *const c_char, provider_name: *const c_char) -> PactHandle {
431+
pub extern "C" fn pactffi_new_pact(consumer_name: *const c_char, provider_name: *const c_char) -> PactHandle {
432432
let consumer = convert_cstr("consumer_name", consumer_name).unwrap_or("Consumer");
433433
let provider = convert_cstr("provider_name", provider_name).unwrap_or("Provider");
434434
PactHandle::new(consumer, provider)
@@ -461,7 +461,7 @@ fn find_interaction_with_description(pact: &V4Pact, description: &str) -> Option
461461
///
462462
/// Returns a new `InteractionHandle`.
463463
#[no_mangle]
464-
pub extern fn pactffi_new_interaction(pact: PactHandle, description: *const c_char) -> InteractionHandle {
464+
pub extern "C" fn pactffi_new_interaction(pact: PactHandle, description: *const c_char) -> InteractionHandle {
465465
if let Some(description) = convert_cstr("description", description) {
466466
pact.with_pact(&|_, inner| {
467467
let interaction = SynchronousHttp {
@@ -490,7 +490,7 @@ pub extern fn pactffi_new_interaction(pact: PactHandle, description: *const c_ch
490490
///
491491
/// Returns a new `InteractionHandle`.
492492
#[no_mangle]
493-
pub extern fn pactffi_new_message_interaction(pact: PactHandle, description: *const c_char) -> InteractionHandle {
493+
pub extern "C" fn pactffi_new_message_interaction(pact: PactHandle, description: *const c_char) -> InteractionHandle {
494494
if let Some(description) = convert_cstr("description", description) {
495495
pact.with_pact(&|_, inner| {
496496
let interaction = AsynchronousMessage {
@@ -519,7 +519,7 @@ pub extern fn pactffi_new_message_interaction(pact: PactHandle, description: *co
519519
///
520520
/// Returns a new `InteractionHandle`.
521521
#[no_mangle]
522-
pub extern fn pactffi_new_sync_message_interaction(pact: PactHandle, description: *const c_char) -> InteractionHandle {
522+
pub extern "C" fn pactffi_new_sync_message_interaction(pact: PactHandle, description: *const c_char) -> InteractionHandle {
523523
if let Some(description) = convert_cstr("description", description) {
524524
pact.with_pact(&|_, inner| {
525525
let interaction = SynchronousMessage {
@@ -545,7 +545,7 @@ pub extern fn pactffi_new_sync_message_interaction(pact: PactHandle, description
545545
///
546546
/// * `description` - The interaction description. It needs to be unique for each interaction.
547547
#[no_mangle]
548-
pub extern fn pactffi_upon_receiving(interaction: InteractionHandle, description: *const c_char) -> bool {
548+
pub extern "C" fn pactffi_upon_receiving(interaction: InteractionHandle, description: *const c_char) -> bool {
549549
if let Some(description) = convert_cstr("description", description) {
550550
interaction.with_interaction(&|_, mock_server_started, inner| {
551551
inner.set_description(description);
@@ -561,7 +561,7 @@ pub extern fn pactffi_upon_receiving(interaction: InteractionHandle, description
561561
///
562562
/// * `description` - The provider state description. It needs to be unique.
563563
#[no_mangle]
564-
pub extern fn pactffi_given(interaction: InteractionHandle, description: *const c_char) -> bool {
564+
pub extern "C" fn pactffi_given(interaction: InteractionHandle, description: *const c_char) -> bool {
565565
if let Some(description) = convert_cstr("description", description) {
566566
interaction.with_interaction(&|_, mock_server_started, inner| {
567567
inner.provider_states_mut().push(ProviderState::default(&description.to_string()));
@@ -619,7 +619,7 @@ ffi_fn! {
619619
/// * `name` - Parameter name.
620620
/// * `value` - Parameter value as JSON.
621621
#[no_mangle]
622-
pub extern fn pactffi_given_with_param(interaction: InteractionHandle, description: *const c_char,
622+
pub extern "C" fn pactffi_given_with_param(interaction: InteractionHandle, description: *const c_char,
623623
name: *const c_char, value: *const c_char) -> bool {
624624
if let Some(description) = convert_cstr("description", description) {
625625
if let Some(name) = convert_cstr("name", name) {
@@ -664,7 +664,7 @@ pub extern fn pactffi_given_with_param(interaction: InteractionHandle, descripti
664664
/// Returns 3 if any of the C strings are not valid.
665665
///
666666
#[no_mangle]
667-
pub extern fn pactffi_given_with_params(
667+
pub extern "C" fn pactffi_given_with_params(
668668
interaction: InteractionHandle,
669669
description: *const c_char,
670670
params: *const c_char
@@ -715,7 +715,7 @@ pub extern fn pactffi_given_with_params(
715715
/// ```
716716
/// See [IntegrationJson.md](https://github.com/pact-foundation/pact-reference/blob/master/rust/pact_ffi/IntegrationJson.md)
717717
#[no_mangle]
718-
pub extern fn pactffi_with_request(
718+
pub extern "C" fn pactffi_with_request(
719719
interaction: InteractionHandle,
720720
method: *const c_char,
721721
path: *const c_char
@@ -753,7 +753,7 @@ pub extern fn pactffi_with_request(
753753
/// **DEPRECATED:** Use `pactffi_with_query_parameter_v2`, which deals with multiple values correctly
754754
#[no_mangle]
755755
#[deprecated]
756-
pub extern fn pactffi_with_query_parameter(
756+
pub extern "C" fn pactffi_with_query_parameter(
757757
interaction: InteractionHandle,
758758
name: *const c_char,
759759
index: size_t,
@@ -863,7 +863,7 @@ pub extern fn pactffi_with_query_parameter(
863863
/// parameter is not NULL, it must point to a valid NULL terminated string.
864864
/// ```
865865
#[no_mangle]
866-
pub extern fn pactffi_with_query_parameter_v2(
866+
pub extern "C" fn pactffi_with_query_parameter_v2(
867867
interaction: InteractionHandle,
868868
name: *const c_char,
869869
index: size_t,
@@ -1107,7 +1107,7 @@ pub(crate) fn process_xml(body: String, matching_rules: &mut MatchingRuleCategor
11071107
/// * `pact` - Handle to a Pact model
11081108
/// * `version` - the spec version to use
11091109
#[no_mangle]
1110-
pub extern fn pactffi_with_specification(pact: PactHandle, version: PactSpecification) -> bool {
1110+
pub extern "C" fn pactffi_with_specification(pact: PactHandle, version: PactSpecification) -> bool {
11111111
pact.with_pact(&|_, inner| {
11121112
inner.specification_version = version.into();
11131113
!inner.mock_server_started
@@ -1136,7 +1136,7 @@ const PROTECTED_NAMES: [&str; 2] = ["pactRust", "pactSpecification"];
11361136
/// * `name` - the key to set
11371137
/// * `value` - the value to set
11381138
#[no_mangle]
1139-
pub extern fn pactffi_with_pact_metadata(
1139+
pub extern "C" fn pactffi_with_pact_metadata(
11401140
pact: PactHandle,
11411141
namespace: *const c_char,
11421142
name: *const c_char,
@@ -1221,7 +1221,7 @@ pub extern fn pactffi_with_pact_metadata(
12211221
/// strings, or `NULL` for the value parameter if the metadata key should be
12221222
/// removed.
12231223
#[no_mangle]
1224-
pub extern fn pactffi_with_metadata(
1224+
pub extern "C" fn pactffi_with_metadata(
12251225
interaction: InteractionHandle,
12261226
key: *const c_char,
12271227
value: *const c_char,
@@ -1327,7 +1327,7 @@ pub extern fn pactffi_with_metadata(
13271327
/// **DEPRECATED:** Use `pactffi_with_header_v2`, which deals with multiple values correctly
13281328
#[deprecated]
13291329
#[no_mangle]
1330-
pub extern fn pactffi_with_header(
1330+
pub extern "C" fn pactffi_with_header(
13311331
interaction: InteractionHandle,
13321332
part: InteractionPart,
13331333
name: *const c_char,
@@ -1437,7 +1437,7 @@ pub extern fn pactffi_with_header(
14371437
/// # Safety
14381438
/// The name and value parameters must be valid pointers to NULL terminated strings.
14391439
#[no_mangle]
1440-
pub extern fn pactffi_with_header_v2(
1440+
pub extern "C" fn pactffi_with_header_v2(
14411441
interaction: InteractionHandle,
14421442
part: InteractionPart,
14431443
name: *const c_char,
@@ -1623,7 +1623,7 @@ ffi_fn! {
16231623
///
16241624
/// * `status` - the response status. Defaults to 200.
16251625
#[no_mangle]
1626-
pub extern fn pactffi_response_status(interaction: InteractionHandle, status: c_ushort) -> bool {
1626+
pub extern "C" fn pactffi_response_status(interaction: InteractionHandle, status: c_ushort) -> bool {
16271627
interaction.with_interaction(&|_, mock_server_started, inner| {
16281628
if let Some(reqres) = inner.as_v4_http_mut() {
16291629
reqres.response.status = status;
@@ -1652,7 +1652,7 @@ pub extern fn pactffi_response_status(interaction: InteractionHandle, status: c_
16521652
/// # Safety
16531653
/// The status parameter must be valid pointers to NULL terminated strings.
16541654
#[no_mangle]
1655-
pub extern fn pactffi_response_status_v2(interaction: InteractionHandle, status: *const c_char) -> bool {
1655+
pub extern "C" fn pactffi_response_status_v2(interaction: InteractionHandle, status: *const c_char) -> bool {
16561656
let status = convert_cstr("status", status).unwrap_or("200");
16571657
interaction.with_interaction(&|_, mock_server_started, inner| {
16581658
if let Some(reqres) = inner.as_v4_http_mut() {
@@ -1977,7 +1977,7 @@ pub extern "C" fn pactffi_with_body(
19771977
/// Returns false if the interaction or Pact can't be modified (i.e. the mock server for it has
19781978
/// already started) or an error has occurred.
19791979
#[no_mangle]
1980-
pub extern fn pactffi_with_binary_body(
1980+
pub extern "C" fn pactffi_with_binary_body(
19811981
interaction: InteractionHandle,
19821982
part: InteractionPart,
19831983
content_type: *const c_char,
@@ -2080,7 +2080,7 @@ pub extern fn pactffi_with_binary_body(
20802080
/// already started) or an error has occurred.
20812081
#[no_mangle]
20822082
#[deprecated(note = "Use `pactffi_with_binary_body` and `pactffi_with_matching_rules` instead")]
2083-
pub extern fn pactffi_with_binary_file(
2083+
pub extern "C" fn pactffi_with_binary_file(
20842084
interaction: InteractionHandle,
20852085
part: InteractionPart,
20862086
content_type: *const c_char,
@@ -2371,7 +2371,7 @@ fn add_content_type_matching_rule_to_body(is_supported: bool, matching_rules: &m
23712371
/// Returns an error if the interaction or Pact can't be modified (i.e. the mock server for it has
23722372
/// already started), the interaction is not an HTTP interaction or some other error occurs.
23732373
#[no_mangle]
2374-
pub extern fn pactffi_with_multipart_file_v2(
2374+
pub extern "C" fn pactffi_with_multipart_file_v2(
23752375
interaction: InteractionHandle,
23762376
part: InteractionPart,
23772377
content_type: *const c_char,
@@ -2462,7 +2462,7 @@ pub extern fn pactffi_with_multipart_file_v2(
24622462
/// Returns an error if the interaction or Pact can't be modified (i.e. the mock server for it has
24632463
/// already started), the interaction is not an HTTP interaction or some other error occurs.
24642464
#[no_mangle]
2465-
pub extern fn pactffi_with_multipart_file(
2465+
pub extern "C" fn pactffi_with_multipart_file(
24662466
interaction: InteractionHandle,
24672467
part: InteractionPart,
24682468
content_type: *const c_char,
@@ -2789,7 +2789,7 @@ ffi_fn! {
27892789
/// Returns a new `MessagePactHandle`. The handle will need to be freed with the `pactffi_free_message_pact_handle`
27902790
/// function to release its resources.
27912791
#[no_mangle]
2792-
pub extern fn pactffi_new_message_pact(consumer_name: *const c_char, provider_name: *const c_char) -> MessagePactHandle {
2792+
pub extern "C" fn pactffi_new_message_pact(consumer_name: *const c_char, provider_name: *const c_char) -> MessagePactHandle {
27932793
let consumer = convert_cstr("consumer_name", consumer_name).unwrap_or("Consumer");
27942794
let provider = convert_cstr("provider_name", provider_name).unwrap_or("Provider");
27952795
MessagePactHandle::new(consumer, provider)
@@ -2801,7 +2801,7 @@ pub extern fn pactffi_new_message_pact(consumer_name: *const c_char, provider_na
28012801
///
28022802
/// Returns a new `MessageHandle`.
28032803
#[no_mangle]
2804-
pub extern fn pactffi_new_message(pact: MessagePactHandle, description: *const c_char) -> MessageHandle {
2804+
pub extern "C" fn pactffi_new_message(pact: MessagePactHandle, description: *const c_char) -> MessageHandle {
28052805
if let Some(description) = convert_cstr("description", description) {
28062806
pact.with_pact(&|_, inner, _| {
28072807
let message = AsynchronousMessage {
@@ -2820,7 +2820,7 @@ pub extern fn pactffi_new_message(pact: MessagePactHandle, description: *const c
28202820
///
28212821
/// * `description` - The message description. It needs to be unique for each message.
28222822
#[no_mangle]
2823-
pub extern fn pactffi_message_expects_to_receive(message: MessageHandle, description: *const c_char) {
2823+
pub extern "C" fn pactffi_message_expects_to_receive(message: MessageHandle, description: *const c_char) {
28242824
if let Some(description) = convert_cstr("description", description) {
28252825
message.with_message(&|_, inner, _| {
28262826
inner.set_description(description);
@@ -2832,7 +2832,7 @@ pub extern fn pactffi_message_expects_to_receive(message: MessageHandle, descrip
28322832
///
28332833
/// * `description` - The provider state description. It needs to be unique for each message
28342834
#[no_mangle]
2835-
pub extern fn pactffi_message_given(message: MessageHandle, description: *const c_char) {
2835+
pub extern "C" fn pactffi_message_given(message: MessageHandle, description: *const c_char) {
28362836
if let Some(description) = convert_cstr("description", description) {
28372837
message.with_message(&|_, inner, _| {
28382838
inner.provider_states_mut().push(ProviderState::default(&description.to_string()));
@@ -2849,7 +2849,7 @@ pub extern fn pactffi_message_given(message: MessageHandle, description: *const
28492849
/// * `name` - Parameter name.
28502850
/// * `value` - Parameter value as JSON.
28512851
#[no_mangle]
2852-
pub extern fn pactffi_message_given_with_param(message: MessageHandle, description: *const c_char,
2852+
pub extern "C" fn pactffi_message_given_with_param(message: MessageHandle, description: *const c_char,
28532853
name: *const c_char, value: *const c_char) {
28542854
if let Some(description) = convert_cstr("description", description) {
28552855
if let Some(name) = convert_cstr("name", name) {
@@ -2887,7 +2887,7 @@ pub extern fn pactffi_message_given_with_param(message: MessageHandle, descripti
28872887
/// * `size` - number of bytes in the message body to read. This is not required for text bodies (JSON, XML, etc.).
28882888
#[no_mangle]
28892889
#[allow(clippy::not_unsafe_ptr_arg_deref)]
2890-
pub extern fn pactffi_message_with_contents(message_handle: MessageHandle, content_type: *const c_char, body: *const u8, size: size_t) {
2890+
pub extern "C" fn pactffi_message_with_contents(message_handle: MessageHandle, content_type: *const c_char, body: *const u8, size: size_t) {
28912891
let content_type = convert_cstr("content_type", content_type).unwrap_or("text/plain");
28922892
trace!("pactffi_message_with_contents(message_handle: {:?}, content_type: {:?}, body: {:?}, size: {})", message_handle, content_type, body, size);
28932893

@@ -2921,7 +2921,7 @@ pub extern fn pactffi_message_with_contents(message_handle: MessageHandle, conte
29212921
/// * `value` - metadata value.
29222922
#[no_mangle]
29232923
#[deprecated(note = "Replaced with `pactffi_with_metadata`")]
2924-
pub extern fn pactffi_message_with_metadata(message_handle: MessageHandle, key: *const c_char, value: *const c_char) {
2924+
pub extern "C" fn pactffi_message_with_metadata(message_handle: MessageHandle, key: *const c_char, value: *const c_char) {
29252925
if let Some(key) = convert_cstr("key", key) {
29262926
let value = convert_cstr("value", value).unwrap_or_default();
29272927
message_handle.with_message(&|_, inner, _| {
@@ -2950,7 +2950,7 @@ pub extern fn pactffi_message_with_metadata(message_handle: MessageHandle, key:
29502950
/// The key and value parameters must be valid pointers to NULL terminated strings.
29512951
#[no_mangle]
29522952
#[deprecated(note = "Replaced with `pactffi_with_metadata`")]
2953-
pub extern fn pactffi_message_with_metadata_v2(message_handle: MessageHandle, key: *const c_char, value: *const c_char) {
2953+
pub extern "C" fn pactffi_message_with_metadata_v2(message_handle: MessageHandle, key: *const c_char, value: *const c_char) {
29542954
if let Some(key) = convert_cstr("key", key) {
29552955
let value = convert_cstr("value", value).unwrap_or_default();
29562956
trace!("pactffi_message_with_metadata_v2(message_handle: {:?}, key: {:?}, value: {})", message_handle, key, value);
@@ -2987,7 +2987,7 @@ pub extern fn pactffi_message_with_metadata_v2(message_handle: MessageHandle, ke
29872987
/// This function must only ever be called from a foreign language. Calling it from a Rust function
29882988
/// that has a Tokio runtime in its call stack can result in a deadlock.
29892989
#[no_mangle]
2990-
pub extern fn pactffi_message_reify(message_handle: MessageHandle) -> *const c_char {
2990+
pub extern "C" fn pactffi_message_reify(message_handle: MessageHandle) -> *const c_char {
29912991
let res = message_handle.with_message(&|_, inner, spec_version| {
29922992
trace!("pactffi_message_reify(message: {:?}, spec_version: {})", inner, spec_version);
29932993
if let Some(message) = inner.as_v4_async_message() {
@@ -3035,7 +3035,7 @@ pub extern fn pactffi_message_reify(message_handle: MessageHandle) -> *const c_c
30353035
/// | 1 | The pact file was not able to be written |
30363036
/// | 2 | The message pact for the given handle was not found |
30373037
#[no_mangle]
3038-
pub extern fn pactffi_write_message_pact_file(pact: MessagePactHandle, directory: *const c_char, overwrite: bool) -> i32 {
3038+
pub extern "C" fn pactffi_write_message_pact_file(pact: MessagePactHandle, directory: *const c_char, overwrite: bool) -> i32 {
30393039
let result = pact.with_pact(&|_, inner, spec_version| {
30403040
let filename = path_from_dir(directory, Some(inner.default_file_name().as_str()));
30413041
write_pact(inner.boxed(), &filename.unwrap_or_else(|| PathBuf::from(inner.default_file_name().as_str())), spec_version, overwrite)
@@ -3063,7 +3063,7 @@ pub extern fn pactffi_write_message_pact_file(pact: MessagePactHandle, directory
30633063
/// * `name` - the key to set
30643064
/// * `value` - the value to set
30653065
#[no_mangle]
3066-
pub extern fn pactffi_with_message_pact_metadata(pact: MessagePactHandle, namespace: *const c_char, name: *const c_char, value: *const c_char) {
3066+
pub extern "C" fn pactffi_with_message_pact_metadata(pact: MessagePactHandle, namespace: *const c_char, name: *const c_char, value: *const c_char) {
30673067
pact.with_pact(&|_, inner, _| {
30683068
let namespace = convert_cstr("namespace", namespace).unwrap_or_default();
30693069
let name = convert_cstr("name", name).unwrap_or_default();
@@ -3163,7 +3163,7 @@ ffi_fn! {
31633163
/// InteractionHandle that can be used for both HTTP and message interactions.
31643164
#[no_mangle]
31653165
#[deprecated(note = "Replaced with new_message_interaction")]
3166-
pub extern fn pactffi_new_async_message(pact: PactHandle, description: *const c_char) -> MessageHandle {
3166+
pub extern "C" fn pactffi_new_async_message(pact: PactHandle, description: *const c_char) -> MessageHandle {
31673167
if let Some(description) = convert_cstr("description", description) {
31683168
pact.with_pact(&|_, inner| {
31693169
let message = AsynchronousMessage {
@@ -3187,7 +3187,7 @@ pub extern fn pactffi_new_async_message(pact: PactHandle, description: *const c_
31873187
/// * `1` - The handle is not valid or does not refer to a valid Pact. Could be that it was previously deleted.
31883188
///
31893189
#[no_mangle]
3190-
pub extern fn pactffi_free_pact_handle(pact: PactHandle) -> c_uint {
3190+
pub extern "C" fn pactffi_free_pact_handle(pact: PactHandle) -> c_uint {
31913191
let mut handles = PACT_HANDLES.lock().unwrap();
31923192
trace!("pactffi_free_pact_handle - removing pact with index {}", pact.pact_ref);
31933193
handles.remove(&pact.pact_ref).map(|_| 0).unwrap_or(1)
@@ -3202,7 +3202,7 @@ pub extern fn pactffi_free_pact_handle(pact: PactHandle) -> c_uint {
32023202
/// * `1` - The handle is not valid or does not refer to a valid Pact. Could be that it was previously deleted.
32033203
///
32043204
#[no_mangle]
3205-
pub extern fn pactffi_free_message_pact_handle(pact: MessagePactHandle) -> c_uint {
3205+
pub extern "C" fn pactffi_free_message_pact_handle(pact: MessagePactHandle) -> c_uint {
32063206
let mut handles = PACT_HANDLES.lock().unwrap();
32073207
handles.remove(&pact.pact_ref).map(|_| 0).unwrap_or(1)
32083208
}

0 commit comments

Comments
 (0)