Skip to content

Commit 167b082

Browse files
committed
refactor: ProfilesDictionary error messages
1 parent 4c866d8 commit 167b082

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

libdd-profiling-ffi/src/profiles/mod.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,11 @@ mod interning_api;
66
mod profiles_dictionary;
77
mod utf8;
88

9-
use std::ffi::CStr;
10-
11-
// Shared error message helpers and null-check macros reused by FFI modules.
12-
pub const fn null_out_param_err() -> &'static CStr {
13-
c"null pointer used as out parameter"
14-
}
15-
16-
pub const fn null_insert_err() -> &'static CStr {
17-
c"tried to insert a null pointer"
18-
}
19-
20-
pub const fn null_profiles_dictionary() -> &'static CStr {
21-
c"passed a null pointer for a ProfilesDictionary"
22-
}
23-
249
#[macro_export]
2510
macro_rules! ensure_non_null_out_parameter {
2611
($expr:expr) => {
2712
if $expr.is_null() {
28-
return $crate::ProfileStatus::from($crate::profiles::null_out_param_err());
13+
return $crate::ProfileStatus::from(c"null pointer used as out parameter");
2914
}
3015
};
3116
}
@@ -34,7 +19,7 @@ macro_rules! ensure_non_null_out_parameter {
3419
macro_rules! ensure_non_null_insert {
3520
($expr:expr) => {
3621
if $expr.is_null() {
37-
return $crate::ProfileStatus::from($crate::profiles::null_insert_err());
22+
return $crate::ProfileStatus::from(c"tried to insert a null pointer");
3823
}
3924
};
4025
}

libdd-profiling-ffi/src/profiles/profiles_dictionary.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
use crate::arc_handle::ArcHandle;
55
use crate::profile_status::ProfileStatus;
66
use crate::profiles::utf8::Utf8Option;
7-
use crate::profiles::{
8-
ensure_non_null_insert, ensure_non_null_out_parameter, null_profiles_dictionary,
9-
};
7+
use crate::profiles::{ensure_non_null_insert, ensure_non_null_out_parameter};
108
use crate::ProfileError;
119
use libdd_common_ffi::slice::CharSlice;
1210
use libdd_profiling::profiles::collections::StringRef;
1311
use libdd_profiling::profiles::datatypes::{
1412
Function2, FunctionId2, Mapping2, MappingId2, ProfilesDictionary, StringId2,
1513
};
14+
use std::ffi::CStr;
1615

1716
/// A StringId that represents the empty string.
1817
/// This is always available in every string set and can be used without
@@ -47,6 +46,8 @@ pub static DDOG_PROF_STRINGID2_TRACE_ENDPOINT: StringId2 =
4746
#[no_mangle]
4847
pub static DDOG_PROF_STRINGID2_SPAN_ID: StringId2 = StringId2::from(StringRef::SPAN_ID);
4948

49+
const NULL_PROFILES_DICTIONARY: &CStr = c"passed a null pointer for a ProfilesDictionary";
50+
5051
/// Allocates a new `ProfilesDictionary` and writes a handle to it in `handle`.
5152
///
5253
/// # Safety
@@ -107,7 +108,7 @@ pub unsafe extern "C" fn ddog_prof_ProfilesDictionary_insert_function(
107108
ensure_non_null_out_parameter!(function_id);
108109
ensure_non_null_insert!(function);
109110
ProfileStatus::from(|| -> Result<(), ProfileError> {
110-
let dict = dict.ok_or(null_profiles_dictionary())?;
111+
let dict = dict.ok_or(NULL_PROFILES_DICTIONARY)?;
111112
let f2: Function2 = unsafe { *function };
112113
let id = dict.try_insert_function2(f2)?;
113114
unsafe { function_id.write(id) };
@@ -131,7 +132,7 @@ pub unsafe extern "C" fn ddog_prof_ProfilesDictionary_insert_mapping(
131132
ensure_non_null_out_parameter!(mapping_id);
132133
ensure_non_null_insert!(mapping);
133134
ProfileStatus::from(|| -> Result<(), ProfileError> {
134-
let dict = dict.ok_or(null_profiles_dictionary())?;
135+
let dict = dict.ok_or(NULL_PROFILES_DICTIONARY)?;
135136
let m2 = unsafe { *mapping };
136137
let id = dict.try_insert_mapping2(m2)?;
137138
unsafe { mapping_id.write(id) };
@@ -156,7 +157,7 @@ pub unsafe extern "C" fn ddog_prof_ProfilesDictionary_insert_str(
156157
) -> ProfileStatus {
157158
ensure_non_null_out_parameter!(string_id);
158159
ProfileStatus::from(|| -> Result<(), ProfileError> {
159-
let dict = dict.ok_or(null_profiles_dictionary())?;
160+
let dict = dict.ok_or(NULL_PROFILES_DICTIONARY)?;
160161
crate::profiles::utf8::insert_str(dict.strings(), byte_slice, utf8_option)
161162
.map(|id| unsafe { string_id.write(id.into()) })
162163
}())
@@ -183,7 +184,7 @@ pub unsafe extern "C" fn ddog_prof_ProfilesDictionary_get_str(
183184
) -> ProfileStatus {
184185
ensure_non_null_out_parameter!(result);
185186
let Some(dict) = dict else {
186-
return ProfileStatus::from(null_profiles_dictionary());
187+
return ProfileStatus::from(NULL_PROFILES_DICTIONARY);
187188
};
188189
let string_ref = StringRef::from(string_id);
189190
// SAFETY: It's not actually safe--as indicated in the docs

0 commit comments

Comments
 (0)