cpp_core/
ops.rs

1//! Operator traits not present in Rust's `std`
2//!
3//! See also `cpp_core::cmp` for comparison operator traits.
4
5// TODO: `&mut self` for increment and decrement?
6
7/// Represents C++'s prefix increment (`++a`).
8pub trait Increment {
9    /// Output type.
10    type Output;
11
12    /// Increment `self`.
13    ///
14    /// # Safety
15    ///
16    /// The caller must make sure `self` contains a valid pointer. This function
17    /// may invoke arbitrary foreign code, so no safety guarantees can be made.
18    unsafe fn inc(&self) -> Self::Output;
19}
20
21/// Represents C++'s prefix decrement (`--a`).
22pub trait Decrement {
23    /// Output type.
24    type Output;
25
26    /// Decrement `self`.
27    ///
28    /// # Safety
29    ///
30    /// The caller must make sure `self` contains a valid pointer. This function
31    /// may invoke arbitrary foreign code, so no safety guarantees can be made.
32    unsafe fn dec(&self) -> Self::Output;
33}
34
35/// Represents C++'s indirection operator (`*a`).
36pub trait Indirection {
37    /// Output type.
38    type Output;
39
40    /// Returns the object `self` is pointing to.
41    ///
42    /// # Safety
43    ///
44    /// The caller must make sure `self` contains a valid pointer. This function
45    /// may invoke arbitrary foreign code, so no safety guarantees can be made.
46    unsafe fn indirection(&self) -> Self::Output;
47}
48
49/// Represents C++'s `begin() const` function.
50pub trait Begin {
51    /// Output type.
52    type Output;
53
54    /// Returns a C++ const iterator object pointing to the beginning of the collection.
55    ///
56    /// # Safety
57    ///
58    /// The caller must make sure `self` contains a valid pointer. This function
59    /// may invoke arbitrary foreign code, so no safety guarantees can be made.
60    unsafe fn begin(&self) -> Self::Output;
61}
62
63/// Represents C++'s `begin()` function.
64pub trait BeginMut {
65    /// Output type.
66    type Output;
67
68    /// Returns a C++ mutable iterator object pointing to the beginning of the collection.
69    ///
70    /// # Safety
71    ///
72    /// The caller must make sure `self` contains a valid pointer. This function
73    /// may invoke arbitrary foreign code, so no safety guarantees can be made.
74    unsafe fn begin_mut(&self) -> Self::Output;
75}
76
77/// Represents C++'s `end() const` function.
78pub trait End {
79    /// Output type.
80    type Output;
81
82    /// Returns a C++ const iterator object pointing to the end of the collection.
83    ///
84    /// # Safety
85    ///
86    /// The caller must make sure `self` contains a valid pointer. This function
87    /// may invoke arbitrary foreign code, so no safety guarantees can be made.
88    unsafe fn end(&self) -> Self::Output;
89}
90
91/// Represents C++'s `end()` function.
92pub trait EndMut {
93    /// Output type.
94    type Output;
95
96    /// Returns a C++ mutable iterator object pointing to the end of the collection.
97    ///
98    /// # Safety
99    ///
100    /// The caller must make sure `self` contains a valid pointer. This function
101    /// may invoke arbitrary foreign code, so no safety guarantees can be made.
102    unsafe fn end_mut(&self) -> Self::Output;
103}