singe-cuda 0.1.0

Safe Rust wrappers for the CUDA driver and runtime APIs, including runtime compilation of CUDA C++ and NVTX annotations.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
//! NVTX annotations, ranges, markers, and profiling resources.

use std::{
    ffi::{CStr, CString},
    marker::PhantomData,
    ptr,
};

use num_enum::{IntoPrimitive, TryFromPrimitive};
use singe_core::{LibraryVersion, impl_enum_display};
use singe_cuda_sys::nvtx as sys;

use crate::error::{Error, Result};

/// A 32-bit annotation color in `0xAARRGGBB` order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Color(u32);

impl Color {
    /// Wraps an annotation color encoded as `0xAARRGGBB`.
    #[must_use]
    pub const fn argb(value: u32) -> Self {
        Self(value)
    }

    /// Packs the red, green, blue, and alpha bytes into native `0xAARRGGBB` order.
    #[must_use]
    pub const fn rgba(red: u8, green: u8, blue: u8, alpha: u8) -> Self {
        Self(((alpha as u32) << 24) | ((red as u32) << 16) | ((green as u32) << 8) | blue as u32)
    }

    /// Returns the packed native ARGB color value.
    #[must_use]
    pub const fn as_raw(self) -> u32 {
        self.0
    }
}

/// An NVTX category ID used to group annotation events within a domain.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Category(u32);

impl Category {
    /// Wraps a category ID without registering a name or selecting a domain.
    #[must_use]
    pub const fn from_raw(value: u32) -> Self {
        Self(value)
    }

    /// Returns the native category ID.
    #[must_use]
    pub const fn as_raw(self) -> u32 {
        self.0
    }
}

singe_core::native_enum! {
    /// Native discriminator selecting whether an annotation includes a color.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
    #[repr(u32)]
    #[non_exhaustive]
    pub enum ColorType: sys::nvtxColorType_t {
        /// Color attribute is unused.
        Unknown = NVTX_COLOR_UNKNOWN,
        /// An ARGB color is provided.
        Argb = NVTX_COLOR_ARGB,
    }
}

impl_enum_display!(ColorType, {
    Self::Unknown => "NVTX_COLOR_UNKNOWN",
    Self::Argb => "NVTX_COLOR_ARGB",
});

singe_core::native_enum! {
    /// Native discriminator selecting the annotation message representation.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
    #[repr(u32)]
    #[non_exhaustive]
    pub enum MessageType: sys::nvtxMessageType_t {
        /// Message attribute is unused.
        Unknown = NVTX_MESSAGE_UNKNOWN,
        /// A character sequence is used as payload.
        Ascii = NVTX_MESSAGE_TYPE_ASCII,
        /// A wide character sequence is used as payload.
        Unicode = NVTX_MESSAGE_TYPE_UNICODE,
        /// A unique string handle registered with a domain.
        Registered = NVTX_MESSAGE_TYPE_REGISTERED,
    }
}

impl_enum_display!(MessageType, {
    Self::Unknown => "NVTX_MESSAGE_UNKNOWN",
    Self::Ascii => "NVTX_MESSAGE_TYPE_ASCII",
    Self::Unicode => "NVTX_MESSAGE_TYPE_UNICODE",
    Self::Registered => "NVTX_MESSAGE_TYPE_REGISTERED",
});

singe_core::native_enum! {
    /// Native discriminator selecting the numeric annotation payload representation.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
    #[repr(u32)]
    #[non_exhaustive]
    pub enum PayloadType: sys::nvtxPayloadType_t {
        /// Payload attribute is unused.
        Unknown = NVTX_PAYLOAD_UNKNOWN,
        /// A 64 bit unsigned integer value is used as payload.
        UnsignedInt64 = NVTX_PAYLOAD_TYPE_UNSIGNED_INT64,
        /// A 64 bit signed integer value is used as payload.
        Int64 = NVTX_PAYLOAD_TYPE_INT64,
        /// A 64 bit floating point value is used as payload.
        Double = NVTX_PAYLOAD_TYPE_DOUBLE,
        /// A 32-bit unsigned integer value is used as payload.
        UnsignedInt32 = NVTX_PAYLOAD_TYPE_UNSIGNED_INT32,
        /// A 32-bit signed integer value is used as payload.
        Int32 = NVTX_PAYLOAD_TYPE_INT32,
        /// A 32 bit floating point value is used as payload.
        Float = NVTX_PAYLOAD_TYPE_FLOAT,
    }
}

impl_enum_display!(PayloadType, {
    Self::Unknown => "NVTX_PAYLOAD_UNKNOWN",
    Self::UnsignedInt64 => "NVTX_PAYLOAD_TYPE_UNSIGNED_INT64",
    Self::Int64 => "NVTX_PAYLOAD_TYPE_INT64",
    Self::Double => "NVTX_PAYLOAD_TYPE_DOUBLE",
    Self::UnsignedInt32 => "NVTX_PAYLOAD_TYPE_UNSIGNED_INT32",
    Self::Int32 => "NVTX_PAYLOAD_TYPE_INT32",
    Self::Float => "NVTX_PAYLOAD_TYPE_FLOAT",
});

singe_core::native_enum! {
    /// Generic resource type for when a resource class is not available.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive, IntoPrimitive)]
    #[repr(u32)]
    #[non_exhaustive]
    pub enum ResourceGenericType: sys::nvtxResourceGenericType_t {
        /// No supported NVTX resource identifier type is selected.
        Unknown = NVTX_RESOURCE_TYPE_UNKNOWN,
        /// Generic pointer assumed to have no collisions with other pointers.
        GenericPointer = NVTX_RESOURCE_TYPE_GENERIC_POINTER,
        /// Generic handle assumed to have no collisions with other handles.
        GenericHandle = NVTX_RESOURCE_TYPE_GENERIC_HANDLE,
        /// OS native thread identifier.
        GenericThreadNative = NVTX_RESOURCE_TYPE_GENERIC_THREAD_NATIVE,
        /// POSIX pthread identifier.
        GenericThreadPosix = NVTX_RESOURCE_TYPE_GENERIC_THREAD_POSIX,
    }
}

impl_enum_display!(ResourceGenericType, {
    Self::Unknown => "NVTX_RESOURCE_TYPE_UNKNOWN",
    Self::GenericPointer => "NVTX_RESOURCE_TYPE_GENERIC_POINTER",
    Self::GenericHandle => "NVTX_RESOURCE_TYPE_GENERIC_HANDLE",
    Self::GenericThreadNative => "NVTX_RESOURCE_TYPE_GENERIC_THREAD_NATIVE",
    Self::GenericThreadPosix => "NVTX_RESOURCE_TYPE_GENERIC_THREAD_POSIX",
});

/// A numeric annotation payload and its native representation.
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub enum Payload {
    /// A signed 32-bit integer payload.
    I32(i32),
    /// A signed 64-bit integer payload.
    I64(i64),
    /// An unsigned 32-bit integer payload.
    U32(u32),
    /// An unsigned 64-bit integer payload.
    U64(u64),
    /// A 32-bit floating-point payload.
    F32(f32),
    /// A 64-bit floating-point payload.
    F64(f64),
}

impl Payload {
    fn encode_type(self) -> sys::nvtxPayloadType_t {
        match self {
            Self::I32(_) => PayloadType::Int32.into(),
            Self::I64(_) => PayloadType::Int64.into(),
            Self::U32(_) => PayloadType::UnsignedInt32.into(),
            Self::U64(_) => PayloadType::UnsignedInt64.into(),
            Self::F32(_) => PayloadType::Float.into(),
            Self::F64(_) => PayloadType::Double.into(),
        }
    }

    fn encode_value(self) -> sys::nvtxEventAttributes_v2_payload_t {
        match self {
            Self::I32(value) => sys::nvtxEventAttributes_v2_payload_t { iValue: value },
            Self::I64(value) => sys::nvtxEventAttributes_v2_payload_t { llValue: value },
            Self::U32(value) => sys::nvtxEventAttributes_v2_payload_t { uiValue: value },
            Self::U64(value) => sys::nvtxEventAttributes_v2_payload_t { ullValue: value },
            Self::F32(value) => sys::nvtxEventAttributes_v2_payload_t { fValue: value },
            Self::F64(value) => sys::nvtxEventAttributes_v2_payload_t { dValue: value },
        }
    }
}

/// Borrowed message and optional metadata for one NVTX marker or range.
///
/// A `&CStr`, `&CString`, or `&Event` converts into attributes with its message and metadata.
/// The message must remain live through the annotation call.
/// Unset color and payload fields use the native unknown selectors, and an unset category uses zero.
#[derive(Debug, Clone, Copy)]
pub struct EventAttributes<'a> {
    message: Option<&'a CStr>,
    category: Option<Category>,
    color: Option<Color>,
    payload: Option<Payload>,
}

impl<'a> EventAttributes<'a> {
    /// Creates attributes with no message, category, color, or payload.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            message: None,
            category: None,
            color: None,
            payload: None,
        }
    }

    /// Sets the borrowed C-string annotation message.
    #[must_use]
    pub fn with_message(mut self, message: &'a CStr) -> Self {
        self.message = Some(message);
        self
    }

    /// Sets the category ID interpreted within the receiving domain.
    #[must_use]
    pub fn with_category(mut self, category: Category) -> Self {
        self.category = Some(category);
        self
    }

    /// Sets the annotation color.
    #[must_use]
    pub fn with_color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Sets the numeric annotation payload.
    #[must_use]
    pub fn with_payload(mut self, payload: Payload) -> Self {
        self.payload = Some(payload);
        self
    }

    /// Returns the borrowed annotation message, if set.
    #[must_use]
    pub const fn message(&self) -> Option<&'a CStr> {
        self.message
    }

    /// Returns the annotation category, if set.
    #[must_use]
    pub const fn category(&self) -> Option<Category> {
        self.category
    }

    /// Returns the annotation color, if set.
    #[must_use]
    pub const fn color(&self) -> Option<Color> {
        self.color
    }

    /// Returns the numeric annotation payload, if set.
    #[must_use]
    pub const fn payload(&self) -> Option<Payload> {
        self.payload
    }

    fn encode(self) -> sys::nvtxEventAttributes_t {
        let mut raw = sys::nvtxEventAttributes_t {
            version: u16::try_from(sys::NVTX_VERSION).expect("the NVTX version fits its ABI field"),
            size: u16::try_from(size_of::<sys::nvtxEventAttributes_t>())
                .expect("the NVTX event attributes fit their ABI size field"),
            ..Default::default()
        };

        if let Some(category) = self.category {
            raw.category = category.0;
        }

        if let Some(color) = self.color {
            raw.colorType = sys::nvtxColorType_t::from(ColorType::Argb).0.cast_signed();
            raw.color = color.0;
        }

        if let Some(payload) = self.payload {
            raw.payloadType = payload.encode_type().0.cast_signed();
            raw.payload = payload.encode_value();
        }

        if let Some(message) = self.message {
            raw.messageType = sys::nvtxMessageType_t::from(MessageType::Ascii)
                .0
                .cast_signed();
            raw.message.ascii = message.as_ptr();
        }

        raw
    }
}

impl Default for EventAttributes<'_> {
    fn default() -> Self {
        Self::new()
    }
}

impl<'a> From<&'a CStr> for EventAttributes<'a> {
    fn from(message: &'a CStr) -> Self {
        Self::new().with_message(message)
    }
}

impl<'a> From<&'a CString> for EventAttributes<'a> {
    fn from(message: &'a CString) -> Self {
        Self::new().with_message(message)
    }
}

impl<'a> From<&'a Event> for EventAttributes<'a> {
    fn from(event: &'a Event) -> Self {
        event.attributes()
    }
}

/// An owned C-string message and reusable NVTX annotation metadata.
///
/// A reference converts into [`EventAttributes`] for the marker and range functions.
#[derive(Debug, Clone)]
pub struct Event {
    message: CString,
    category: Option<Category>,
    color: Option<Color>,
    payload: Option<Payload>,
}

impl Event {
    /// Copies `message` into an owned C string with no optional annotation metadata.
    ///
    /// # Errors
    ///
    /// Returns an error if `message` contains an interior NUL byte.
    pub fn create(message: &str) -> Result<Self> {
        Ok(Self::create_from_c_string(CString::new(message)?))
    }

    /// Takes ownership of a C-string message with no optional annotation metadata.
    #[must_use]
    pub fn create_from_c_string(message: CString) -> Self {
        Self {
            message,
            category: None,
            color: None,
            payload: None,
        }
    }

    /// Sets the category used by markers and ranges created from this event.
    #[must_use]
    pub fn with_category(mut self, category: Category) -> Self {
        self.category = Some(category);
        self
    }

    /// Sets the color used by markers and ranges created from this event.
    #[must_use]
    pub fn with_color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Sets the numeric payload used by markers and ranges created from this event.
    #[must_use]
    pub fn with_payload(mut self, payload: Payload) -> Self {
        self.payload = Some(payload);
        self
    }

    /// Borrows the message and copies the optional metadata into annotation attributes.
    #[must_use]
    pub fn attributes(&self) -> EventAttributes<'_> {
        EventAttributes {
            message: Some(&self.message),
            category: self.category,
            color: self.color,
            payload: self.payload,
        }
    }
}

/// An owned NVTX annotation domain.
///
/// A domain scopes category names and annotations and maintains its own per-thread range stacks.
/// Dropping this wrapper destroys the native domain; domain range guards borrow it.
#[derive(Debug)]
pub struct Domain {
    handle: sys::nvtxDomainHandle_t,
}

// SAFETY: an NVTX domain handle is process-wide and NVTX entry points are thread-safe, so the owner may move and destroy it on any thread.
unsafe impl Send for Domain {}

// SAFETY: shared methods only pass the handle by value to thread-safe NVTX entry points; per-thread range stacks are kept by NVTX itself.
unsafe impl Sync for Domain {}

impl Domain {
    /// Registers a named NVTX annotation domain.
    ///
    /// # Errors
    ///
    /// Returns an error if `name` contains an interior NUL byte or NVTX returns a null domain handle.
    pub fn create(name: &str) -> Result<Self> {
        let name = CString::new(name)?;
        Self::create_from_c_str(&name)
    }

    /// Registers an NVTX annotation domain using an existing C-string name.
    ///
    /// # Errors
    ///
    /// Returns an error if NVTX returns a null domain handle.
    pub fn create_from_c_str(name: &CStr) -> Result<Self> {
        // SAFETY: `name` is a NUL-terminated string borrowed for the call.
        let handle = unsafe { sys::nvtxDomainCreateA(name.as_ptr()) };
        if handle.is_null() {
            return Err(Error::NullHandle);
        }
        Ok(Self { handle })
    }

    /// Returns the borrowed native domain handle without transferring ownership.
    #[must_use]
    pub fn as_raw(&self) -> sys::nvtxDomainHandle_t {
        self.handle
    }

    /// Emits an instantaneous marker in this domain.
    ///
    /// `attributes` is a C-string message, an [`Event`], or [`EventAttributes`] with metadata.
    pub fn mark<'a>(&self, attributes: impl Into<EventAttributes<'a>>) {
        let raw = attributes.into().encode();
        // SAFETY: `self` owns the live domain, and `raw` and the message it points to are borrowed for the call.
        unsafe { sys::nvtxDomainMarkEx(self.handle, &raw const raw) };
    }

    /// Pushes a range onto this domain's current-thread stack and returns its pop-on-drop guard.
    ///
    /// `attributes` is a C-string message, an [`Event`], or [`EventAttributes`] with metadata.
    #[must_use]
    pub fn range<'a>(&self, attributes: impl Into<EventAttributes<'a>>) -> DomainLocalRange<'_> {
        let raw = attributes.into().encode();
        // SAFETY: `self` owns the live domain, and `raw` and the message it points to are borrowed for the call.
        unsafe { sys::nvtxDomainRangePushEx(self.handle, &raw const raw) };
        DomainLocalRange {
            domain: self,
            _not_send: PhantomData,
        }
    }

    /// Starts a process range in this domain and returns a guard that ends its native ID on drop.
    ///
    /// `attributes` is a C-string message, an [`Event`], or [`EventAttributes`] with metadata.
    #[must_use]
    pub fn start_range<'a>(&self, attributes: impl Into<EventAttributes<'a>>) -> DomainRange<'_> {
        let raw = attributes.into().encode();
        // SAFETY: `self` owns the live domain, and `raw` and the message it points to are borrowed for the call.
        let id = unsafe { sys::nvtxDomainRangeStartEx(self.handle, &raw const raw) };
        DomainRange { domain: self, id }
    }

    /// Assigns `name` to a category ID within this domain.
    ///
    /// # Errors
    ///
    /// Returns an error if `name` contains an interior NUL byte.
    pub fn name_category(&self, category: Category, name: &str) -> Result<()> {
        let name = CString::new(name)?;
        // SAFETY: `self` owns the live domain, and `name` is a NUL-terminated string borrowed for the call.
        unsafe { sys::nvtxDomainNameCategoryA(self.handle, category.0, name.as_ptr()) };
        Ok(())
    }
}

impl Drop for Domain {
    fn drop(&mut self) {
        // SAFETY: this wrapper owns the domain, and range guards borrow it, so none outlives the destroy.
        unsafe { sys::nvtxDomainDestroy(self.handle) };
    }
}

/// A guard for one range on the global domain's current-thread stack.
///
/// Dropping the guard pops the most recently pushed range on that thread.
/// Guards must be dropped in reverse push order and cannot move to another thread.
#[derive(Debug)]
pub struct LocalRange {
    _not_send: PhantomData<*mut ()>,
}

impl LocalRange {
    /// Pushes a range onto the global domain's current-thread stack and returns a guard that ends it on drop.
    ///
    /// `attributes` is a C-string message, an [`Event`], or [`EventAttributes`] with metadata.
    #[must_use]
    pub fn push<'a>(attributes: impl Into<EventAttributes<'a>>) -> Self {
        let raw = attributes.into().encode();
        // SAFETY: `raw` and the message it points to are borrowed for the call.
        unsafe { sys::nvtxRangePushEx(&raw const raw) };
        Self {
            _not_send: PhantomData,
        }
    }
}

impl Drop for LocalRange {
    fn drop(&mut self) {
        // SAFETY: the guard is `!Send`, so this pops the range it pushed on the current thread's stack.
        unsafe { sys::nvtxRangePop() };
    }
}

/// A guard for a process range in the global NVTX domain.
///
/// Dropping the guard ends the range by its native ID.
/// These ranges need not be nested and can end on another thread.
#[derive(Debug)]
pub struct Range {
    id: sys::nvtxRangeId_t,
}

impl Range {
    /// Starts a process range in the global NVTX domain and returns a guard that ends it on drop.
    ///
    /// `attributes` is a C-string message, an [`Event`], or [`EventAttributes`] with metadata.
    #[must_use]
    pub fn start<'a>(attributes: impl Into<EventAttributes<'a>>) -> Self {
        let raw = attributes.into().encode();
        // SAFETY: `raw` and the message it points to are borrowed for the call.
        let id = unsafe { sys::nvtxRangeStartEx(&raw const raw) };
        Self { id }
    }
}

impl Drop for Range {
    fn drop(&mut self) {
        // SAFETY: `self.id` is the range this guard started, ended exactly once here.
        unsafe { sys::nvtxRangeEnd(self.id) };
    }
}

/// A guard for a range on a domain's current-thread stack.
///
/// Dropping the guard pops that domain's most recently pushed range on the thread.
/// Guards must be dropped in reverse push order and cannot move to another thread.
#[derive(Debug)]
pub struct DomainLocalRange<'a> {
    domain: &'a Domain,
    _not_send: PhantomData<*mut ()>,
}

impl Drop for DomainLocalRange<'_> {
    fn drop(&mut self) {
        // SAFETY: the borrowed domain is live, and the `!Send` guard pops the range it pushed on this thread.
        unsafe { sys::nvtxDomainRangePop(self.domain.handle) };
    }
}

/// A process-range guard borrowing its NVTX domain.
///
/// Dropping the guard ends its native range ID.
/// These ranges need not be nested and can end on another thread.
#[derive(Debug)]
pub struct DomainRange<'a> {
    domain: &'a Domain,
    id: sys::nvtxRangeId_t,
}

impl Drop for DomainRange<'_> {
    fn drop(&mut self) {
        // SAFETY: the borrowed domain is live, and `self.id` is the range this guard started in it.
        unsafe { sys::nvtxDomainRangeEnd(self.domain.handle, self.id) };
    }
}

/// Returns the compile-time NVTX API version used by this crate.
#[must_use]
pub fn version() -> LibraryVersion {
    LibraryVersion::new(sys::NVTX_VERSION, 0, 0)
}

/// Initializes NVTX before the first annotation call.
///
/// Other NVTX calls initialize the API lazily if this function has not been called.
pub fn initialize() {
    // SAFETY: NVTX accepts a null reserved argument.
    unsafe { sys::nvtxInitialize(ptr::null()) };
}

/// Emits an instantaneous marker in the global NVTX domain.
///
/// `attributes` is a C-string message, an [`Event`], or [`EventAttributes`] with metadata.
pub fn mark<'a>(attributes: impl Into<EventAttributes<'a>>) {
    let raw = attributes.into().encode();
    // SAFETY: `raw` and the message it points to are borrowed for the call.
    unsafe { sys::nvtxMarkEx(&raw const raw) };
}

/// Assigns `name` to a category ID in the global NVTX domain.
///
/// # Errors
///
/// Returns an error if `name` contains an interior NUL byte.
pub fn name_category(category: Category, name: &str) -> Result<()> {
    let name = CString::new(name)?;
    // SAFETY: `name` is a NUL-terminated string borrowed for the call.
    unsafe { sys::nvtxNameCategoryA(category.0, name.as_ptr()) };
    Ok(())
}

/// Assigns `name` to an active operating-system thread in the current process.
///
/// Use the operating system's native numeric thread ID.
/// A POSIX `pthread_t` need not have the required representation.
/// Tool behavior is unspecified for invalid IDs or IDs from another process.
///
/// # Errors
///
/// Returns an error if `name` contains an interior NUL byte.
/// The wrapper does not check that the thread ID is valid.
pub fn name_os_thread(thread_id: u32, name: &str) -> Result<()> {
    let name = CString::new(name)?;
    // SAFETY: `name` is a NUL-terminated string borrowed for the call, and NVTX does not dereference the thread ID.
    unsafe { sys::nvtxNameOsThreadA(thread_id, name.as_ptr()) };
    Ok(())
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn test_encodes_event_attributes() {
        let message = c"work";
        let raw = EventAttributes::new()
            .with_message(message)
            .with_category(Category::from_raw(7))
            .with_color(Color::rgba(1, 2, 3, 4))
            .with_payload(Payload::I64(-42))
            .encode();

        assert_eq!(
            raw.version,
            u16::try_from(sys::NVTX_VERSION).expect("the NVTX version fits its ABI field")
        );
        assert_eq!(
            raw.size,
            u16::try_from(size_of::<sys::nvtxEventAttributes_t>())
                .expect("the NVTX event attributes fit their ABI size field")
        );
        assert_eq!(raw.category, 7);
        assert_eq!(
            raw.colorType,
            sys::nvtxColorType_t::NVTX_COLOR_ARGB.0.cast_signed()
        );
        assert_eq!(raw.color, 0x0401_0203);
        assert_eq!(
            raw.messageType,
            sys::nvtxMessageType_t::NVTX_MESSAGE_TYPE_ASCII
                .0
                .cast_signed()
        );
        // SAFETY: the message type checked above selects `ascii`.
        assert_eq!(unsafe { raw.message.ascii }, message.as_ptr());
        assert_eq!(
            raw.payloadType,
            sys::nvtxPayloadType_t::NVTX_PAYLOAD_TYPE_INT64
                .0
                .cast_signed()
        );
        // SAFETY: the payload type checked above selects `llValue`.
        assert_eq!(unsafe { raw.payload.llValue }, -42);
    }

    #[test]
    fn test_owned_event_builds_attributes() {
        let event = Event::create("owned")
            .unwrap()
            .with_category(Category::from_raw(3))
            .with_color(Color::argb(0xff00_00ff))
            .with_payload(Payload::U32(11));

        let attributes = event.attributes();
        let raw = attributes.encode();

        assert_eq!(attributes.message(), Some(c"owned"));
        assert_eq!(attributes.category(), Some(Category::from_raw(3)));
        assert_eq!(attributes.color(), Some(Color::argb(0xff00_00ff)));
        assert_eq!(attributes.payload(), Some(Payload::U32(11)));
        assert_eq!(raw.category, 3);
        assert_eq!(raw.color, 0xff00_00ff);
        assert_eq!(
            raw.payloadType,
            sys::nvtxPayloadType_t::NVTX_PAYLOAD_TYPE_UNSIGNED_INT32
                .0
                .cast_signed()
        );
        // SAFETY: the payload type checked above selects `uiValue`.
        assert_eq!(unsafe { raw.payload.uiValue }, 11);
    }

    #[test]
    fn test_enum_wrappers_convert_and_display() {
        assert_eq!(
            ColorType::try_from(sys::nvtxColorType_t::NVTX_COLOR_ARGB).unwrap(),
            ColorType::Argb
        );
        assert_eq!(
            sys::nvtxMessageType_t::from(MessageType::Ascii),
            sys::nvtxMessageType_t::NVTX_MESSAGE_TYPE_ASCII
        );
        assert_eq!(
            PayloadType::UnsignedInt64.to_string(),
            "NVTX_PAYLOAD_TYPE_UNSIGNED_INT64"
        );
        assert_eq!(
            ResourceGenericType::GenericThreadPosix.to_string(),
            "NVTX_RESOURCE_TYPE_GENERIC_THREAD_POSIX"
        );
    }
}