@@ -70,6 +70,15 @@ impl From<anyhow::Result<()>> for ProfileResult {
70
70
}
71
71
}
72
72
73
+ impl From < ProfileResult > for Result < ( ) , Error > {
74
+ fn from ( result : ProfileResult ) -> Self {
75
+ match result {
76
+ ProfileResult :: Ok ( _) => Ok ( ( ) ) ,
77
+ ProfileResult :: Err ( err) => Err ( err) ,
78
+ }
79
+ }
80
+ }
81
+
73
82
/// Returned by [ddog_prof_Profile_new].
74
83
#[ allow( dead_code) ]
75
84
#[ repr( C ) ]
@@ -79,6 +88,15 @@ pub enum ProfileNewResult {
79
88
Err ( Error ) ,
80
89
}
81
90
91
+ impl From < ProfileNewResult > for Result < Profile , Error > {
92
+ fn from ( value : ProfileNewResult ) -> Self {
93
+ match value {
94
+ ProfileNewResult :: Ok ( v) => Ok ( v) ,
95
+ ProfileNewResult :: Err ( e) => Err ( e) ,
96
+ }
97
+ }
98
+ }
99
+
82
100
#[ allow( dead_code) ]
83
101
#[ repr( C ) ]
84
102
pub enum SerializeResult {
@@ -390,7 +408,7 @@ impl<'a> From<Sample<'a>> for api::StringIdSample<'a> {
390
408
/// time.
391
409
///
392
410
/// # Safety
393
- /// All slices must be have pointers that are suitably aligned for their type
411
+ /// All slices must have pointers that are suitably aligned for their type
394
412
/// and must have the correct number of elements for the slice.
395
413
#[ no_mangle]
396
414
#[ must_use]
@@ -401,7 +419,11 @@ pub unsafe extern "C" fn ddog_prof_Profile_new(
401
419
profile_new ( sample_types, period, None )
402
420
}
403
421
404
- /// Same as `ddog_profile_new` but also configures a `string_storage` for the profile.
422
+ /// Same as [ddog_prof_Profile_new] but also configures a `string_storage` for
423
+ /// the profile.
424
+ ///
425
+ /// # Safety
426
+ /// Has all same safety conditions as [ddog_prof_Profile_new].
405
427
#[ no_mangle]
406
428
#[ must_use]
407
429
/// TODO: @ivoanjo Should this take a `*mut ManagedStringStorage` like Profile APIs do?
@@ -447,26 +469,6 @@ pub unsafe extern "C" fn ddog_prof_Profile_drop(profile: *mut Profile) {
447
469
}
448
470
}
449
471
450
- #[ cfg( test) ]
451
- impl From < ProfileResult > for Result < ( ) , Error > {
452
- fn from ( result : ProfileResult ) -> Self {
453
- match result {
454
- ProfileResult :: Ok ( _) => Ok ( ( ) ) ,
455
- ProfileResult :: Err ( err) => Err ( err) ,
456
- }
457
- }
458
- }
459
-
460
- #[ cfg( test) ]
461
- impl From < ProfileNewResult > for Result < Profile , Error > {
462
- fn from ( result : ProfileNewResult ) -> Self {
463
- match result {
464
- ProfileNewResult :: Ok ( p) => Ok ( p) ,
465
- ProfileNewResult :: Err ( err) => Err ( err) ,
466
- }
467
- }
468
- }
469
-
470
472
/// # Safety
471
473
/// The `profile` ptr must point to a valid Profile object created by this
472
474
/// module. All pointers inside the `sample` need to be valid for the duration
@@ -547,6 +549,58 @@ pub unsafe extern "C" fn ddog_prof_Profile_set_endpoint(
547
549
. into ( )
548
550
}
549
551
552
+ /// Sets the profile's upload compression algorithm. Useful only for testing.
553
+ ///
554
+ /// # Errors
555
+ ///
556
+ /// Returns an error if either the pointer or the inner profiler ptr is null.
557
+ ///
558
+ /// # Safety
559
+ ///
560
+ /// The `profile` ptr must point to a valid Profile object.
561
+ ///
562
+ /// # Examples
563
+ ///
564
+ /// ```
565
+ /// # use datadog_profiling_ffi::{Error, ddog_prof_Profile_set_upload_compression, ddog_prof_Profile_new, ValueType, Slice, CharSlice};
566
+ /// # use std::ptr::addr_of_mut;
567
+ /// # fn main() -> Result<(), Error> { unsafe {
568
+ /// use datadog_profiling::UploadCompression;
569
+ ///
570
+ /// let mut profile = Result::from(ddog_prof_Profile_new(
571
+ /// Slice::from([ValueType {
572
+ /// type_: CharSlice::from("sample"),
573
+ /// unit: CharSlice::from("count"),
574
+ /// }].as_slice()),
575
+ /// None,
576
+ /// ))?;
577
+ ///
578
+ /// // Set compression off (only for testing).
579
+ /// Result::from(ddog_prof_Profile_set_upload_compression(
580
+ /// addr_of_mut!(profile),
581
+ /// UploadCompression::Off
582
+ /// ))?;
583
+ /// # } Ok(()) }
584
+ /// ```
585
+ #[ no_mangle]
586
+ #[ must_use]
587
+ #[ named]
588
+ pub unsafe extern "C" fn ddog_prof_Profile_set_upload_compression (
589
+ profile : * mut Profile ,
590
+ upload_compression : datadog_profiling:: UploadCompression ,
591
+ ) -> ProfileResult {
592
+ match profile_ptr_to_inner ( profile) {
593
+ Ok ( profile) => {
594
+ profile. set_upload_compression ( upload_compression) ;
595
+ ProfileResult :: Ok ( true )
596
+ }
597
+ Err ( err) => {
598
+ let e = err. context ( concat ! ( function_name!( ) , " failed" ) ) ;
599
+ ProfileResult :: Err ( e. into ( ) )
600
+ }
601
+ }
602
+ }
603
+
550
604
/// Count the number of times an endpoint has been seen.
551
605
///
552
606
/// # Arguments
@@ -759,6 +813,14 @@ pub unsafe extern "C" fn ddog_prof_Profile_serialize(
759
813
. into ( )
760
814
}
761
815
816
+ /// Borrows FFI Vec as an FFI Slice.
817
+ ///
818
+ /// # Safety
819
+ /// The input needs to be a valid reference to an FFI Vec, and the slice needs
820
+ /// to be used in a way consistent with the lifetime and safety rules. Some
821
+ /// things to avoid:
822
+ /// - Do not modify the Vec at all while the Slice is alive.
823
+ /// - Do not drop the Vec while the Slice is alive.
762
824
#[ must_use]
763
825
#[ no_mangle]
764
826
pub unsafe extern "C" fn ddog_Vec_U8_as_slice ( vec : & ddcommon_ffi:: Vec < u8 > ) -> Slice < u8 > {
0 commit comments