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
use ;
use Deref;
/// Type to represent an offset that will be applied to the current timestamp in order to update
/// the `vt` (visibility timeout) timestamp column of a queue message. Used by various
/// methods in [`crate::pg_ext::PGMQueueExt`] to set the visibility timeout of a job. Supports
/// converting from [`chrono::Duration`], [`std::time::Duration`], and various integer types
/// (assumed to be a duration in seconds).
///
/// Note: The offset has 1 second precision and is stored as an [`i32`]. This limits the possible
/// range of values compared to what's technically supported by Postgres. However, this ensures
/// that the value provided to Postgres will not overflow, and the maximum [`i32`] value in seconds
/// is roughly 68 years, which should be plenty large for virtually any use case. If any conversion
/// to [`i32`] would result in an overflow, the value is instead capped to
/// [`i32::MIN`]/[`i32::MAX`].
///
/// # Examples
///
/// ## Convert from `i32`
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(10i32, *VisibilityTimeoutOffset::from(10i32));
/// ```
///
/// ## Convert from `u32`
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(10i32, *VisibilityTimeoutOffset::from(10u32));
/// ```
///
/// ## Convert from `u32` capped
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(i32::MAX, *VisibilityTimeoutOffset::from(u32::MAX));
/// ```
///
/// ## Convert from `i64`
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(10i32, *VisibilityTimeoutOffset::from(10i64));
/// ```
///
/// ## Convert from `i64` capped max
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(i32::MAX, *VisibilityTimeoutOffset::from(i64::MAX));
/// ```
///
/// ## Convert from `i64` capped min
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(i32::MIN, *VisibilityTimeoutOffset::from(i64::MIN));
/// ```
///
/// ## Convert from [`chrono::Duration`]`
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(10i32, *VisibilityTimeoutOffset::from(chrono::Duration::seconds(10)));
/// ```
///
/// ## Convert from [`chrono::Duration`]` capped max
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(VisibilityTimeoutOffset::MAX, VisibilityTimeoutOffset::from(chrono::Duration::MAX));
/// ```
///
/// ## Convert from [`chrono::Duration`]` capped min
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(VisibilityTimeoutOffset::MAX, VisibilityTimeoutOffset::from(chrono::Duration::MAX));
/// ```
///
/// ## Convert from [`std::time::Duration`]`
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(10i32, *VisibilityTimeoutOffset::from(std::time::Duration::from_secs(10)));
/// ```
///
/// ## Convert from [`std::time::Duration`]` capped max
/// ```
/// # use pgmq::pg_ext::VisibilityTimeoutOffset;
/// assert_eq!(VisibilityTimeoutOffset::MAX, VisibilityTimeoutOffset::from(std::time::Duration::MAX));
/// ```
;
/*
Manually implement `sqlx::Type` because the derive macro automatically implements both
`sqlx::Encode` and `sqlx::Decode`, but we only need `sqlx::Encode` for this type.
However, `sqlx::Encode` is implemented via a derive macro.
*/