Skip to content

Commit 8a0c739

Browse files
committed
Add optional serde serialization support
- Update ci to run serde tests - Add serialization support for Enums TODO - Structs
1 parent 653ef75 commit 8a0c739

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ jobs:
4848
export AF_PATH=${GITHUB_WORKSPACE}/afbin
4949
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${AF_PATH}/lib64
5050
echo "Using cargo version: $(cargo --version)"
51-
cargo build --all
52-
cargo test --no-fail-fast
51+
cargo build --all --features="afserde"
52+
cargo test --no-fail-fast --features="afserde"
5353
5454
format:
5555
name: Format Check

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,18 @@ statistics = []
4646
vision = []
4747
default = ["algorithm", "arithmetic", "blas", "data", "indexing", "graphics", "image", "lapack",
4848
"ml", "macros", "random", "signal", "sparse", "statistics", "vision"]
49+
afserde = ["serde"]
4950

5051
[dependencies]
5152
libc = "0.2"
5253
num = "0.2"
5354
lazy_static = "1.0"
5455
half = "1.5.0"
56+
serde = { version = "1.0", features = ["derive"], optional = true }
5557

5658
[dev-dependencies]
5759
half = "1.5.0"
60+
serde_json = "1.0"
5861

5962
[build-dependencies]
6063
serde_json = "1.0"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Only, Major(M) & Minor(m) version numbers need to match. *p1* and *p2* are patch
1616

1717
## Supported platforms
1818

19-
Linux, Windows and OSX. Rust 1.15.1 or higher is required.
19+
Linux, Windows and OSX. Rust 1.31 or newer is required.
2020

2121
## Use from Crates.io [![][6]][7] [![][8]][9]
2222

src/core/defines.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ use num::Complex;
22
use std::fmt::Error as FmtError;
33
use std::fmt::{Display, Formatter};
44

5+
#[cfg(feature = "afserde")]
6+
use serde::{Serialize, Deserialize};
7+
58
/// Error codes
69
#[repr(u32)]
710
#[derive(Clone, Copy, Debug, PartialEq)]
11+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
812
pub enum AfError {
913
/// The function returned successfully
1014
SUCCESS = 0,
@@ -52,6 +56,7 @@ pub enum AfError {
5256
/// Compute/Acceleration Backend
5357
#[repr(u32)]
5458
#[derive(Clone, Copy, Debug, PartialEq)]
59+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
5560
pub enum Backend {
5661
/// Default backend order: OpenCL -> CUDA -> CPU
5762
DEFAULT = 0,
@@ -103,6 +108,7 @@ impl Display for AfError {
103108
/// Types of Array data type
104109
#[repr(u32)]
105110
#[derive(Clone, Copy, Debug, PartialEq)]
111+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
106112
pub enum DType {
107113
/// 32 bit float
108114
F32 = 0,
@@ -135,6 +141,7 @@ pub enum DType {
135141
/// Dictates the interpolation method to be used by a function
136142
#[repr(u32)]
137143
#[derive(Clone, Copy, Debug, PartialEq)]
144+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
138145
pub enum InterpType {
139146
/// Nearest Neighbor interpolation method
140147
NEAREST = 0,
@@ -161,6 +168,7 @@ pub enum InterpType {
161168
/// Helps determine how to pad kernels along borders
162169
#[repr(u32)]
163170
#[derive(Clone, Copy, Debug, PartialEq)]
171+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
164172
pub enum BorderType {
165173
/// Pad using zeros
166174
ZERO = 0,
@@ -177,6 +185,7 @@ pub enum BorderType {
177185
/// Used by `regions` function to identify type of connectivity
178186
#[repr(u32)]
179187
#[derive(Clone, Copy, Debug, PartialEq)]
188+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
180189
pub enum Connectivity {
181190
/// North-East-South-West (N-E-S-W) connectivity from given pixel/point
182191
FOUR = 4,
@@ -187,6 +196,7 @@ pub enum Connectivity {
187196
/// Helps determine the size of output of convolution
188197
#[repr(u32)]
189198
#[derive(Clone, Copy, Debug, PartialEq)]
199+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
190200
pub enum ConvMode {
191201
/// Default convolution mode where output size is same as input size
192202
DEFAULT = 0,
@@ -197,6 +207,7 @@ pub enum ConvMode {
197207
/// Helps determine if convolution is in Spatial or Frequency domain
198208
#[repr(u32)]
199209
#[derive(Clone, Copy, Debug, PartialEq)]
210+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
200211
pub enum ConvDomain {
201212
/// ArrayFire chooses whether the convolution will be in spatial domain or frequency domain
202213
AUTO = 0,
@@ -209,6 +220,7 @@ pub enum ConvDomain {
209220
/// Error metric used by `matchTemplate` function
210221
#[repr(u32)]
211222
#[derive(Clone, Copy, Debug, PartialEq)]
223+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
212224
pub enum MatchType {
213225
/// Sum of Absolute Differences
214226
SAD = 0,
@@ -233,6 +245,7 @@ pub enum MatchType {
233245
/// Identify the color space of given image(Array)
234246
#[repr(u32)]
235247
#[derive(Clone, Copy, Debug, PartialEq)]
248+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
236249
pub enum ColorSpace {
237250
/// Grayscale color space
238251
GRAY = 0,
@@ -245,6 +258,7 @@ pub enum ColorSpace {
245258
/// Helps determine the type of a Matrix
246259
#[repr(u32)]
247260
#[derive(Clone, Copy, Debug, PartialEq)]
261+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
248262
pub enum MatProp {
249263
/// Default (no-op)
250264
NONE = 0,
@@ -276,6 +290,7 @@ pub enum MatProp {
276290
#[allow(non_camel_case_types)]
277291
#[repr(u32)]
278292
#[derive(Clone, Copy, Debug, PartialEq)]
293+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
279294
pub enum NormType {
280295
/// Treats input as a vector and return sum of absolute values
281296
VECTOR_1 = 0,
@@ -298,6 +313,7 @@ pub enum NormType {
298313
/// Dictates what color map is used for Image rendering
299314
#[repr(u32)]
300315
#[derive(Clone, Copy, Debug, PartialEq)]
316+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
301317
pub enum ColorMap {
302318
/// Default color map is grayscale range [0-1]
303319
DEFAULT = 0,
@@ -318,6 +334,7 @@ pub enum ColorMap {
318334
/// YCbCr Standards
319335
#[repr(u32)]
320336
#[derive(Clone, Copy, Debug, PartialEq)]
337+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
321338
pub enum YCCStd {
322339
/// ITU-R BT.601 (formerly CCIR 601) standard
323340
YCC_601 = 601,
@@ -330,6 +347,7 @@ pub enum YCCStd {
330347
/// Homography type
331348
#[repr(u32)]
332349
#[derive(Clone, Copy, Debug, PartialEq)]
350+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
333351
pub enum HomographyType {
334352
/// RANdom SAmple Consensus algorithm
335353
RANSAC = 0,
@@ -340,6 +358,7 @@ pub enum HomographyType {
340358
/// Plotting markers
341359
#[repr(u32)]
342360
#[derive(Clone, Copy, Debug, PartialEq)]
361+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
343362
pub enum MarkerType {
344363
/// No marker
345364
NONE = 0,
@@ -362,6 +381,7 @@ pub enum MarkerType {
362381
/// Image moment types
363382
#[repr(u32)]
364383
#[derive(Clone, Copy, Debug, PartialEq)]
384+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
365385
pub enum MomentType {
366386
/// Central moment of order (0 + 0)
367387
M00 = 1, // 1<<0
@@ -378,6 +398,7 @@ pub enum MomentType {
378398
/// Sparse storage format type
379399
#[repr(u32)]
380400
#[derive(Clone, Copy, Debug, PartialEq)]
401+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
381402
pub enum SparseFormat {
382403
/// Dense format
383404
DENSE = 0,
@@ -392,6 +413,7 @@ pub enum SparseFormat {
392413
/// Binary operation types for generalized scan functions
393414
#[repr(u32)]
394415
#[derive(Clone, Copy, Debug, PartialEq)]
416+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
395417
pub enum BinaryOp {
396418
/// Addition operation
397419
ADD = 0,
@@ -406,6 +428,7 @@ pub enum BinaryOp {
406428
/// Random engine types
407429
#[repr(u32)]
408430
#[derive(Clone, Copy, Debug, PartialEq)]
431+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
409432
pub enum RandomEngineType {
410433
///Philox variant with N=4, W=32 and Rounds=10
411434
PHILOX_4X32_10 = 100,
@@ -456,6 +479,7 @@ pub enum Scalar {
456479
/// Canny edge detector threshold operations types
457480
#[repr(u32)]
458481
#[derive(Clone, Copy, Debug, PartialEq)]
482+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
459483
pub enum CannyThresholdType {
460484
/// User has to define canny thresholds manually
461485
MANUAL = 0,
@@ -466,6 +490,7 @@ pub enum CannyThresholdType {
466490
/// Anisotropic diffusion flux equation types
467491
#[repr(u32)]
468492
#[derive(Clone, Copy, Debug, PartialEq)]
493+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
469494
pub enum DiffusionEq {
470495
/// Quadratic flux function
471496
QUADRATIC = 1,
@@ -478,6 +503,7 @@ pub enum DiffusionEq {
478503
/// Diffusion equation types
479504
#[repr(u32)]
480505
#[derive(Clone, Copy, Debug, PartialEq)]
506+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
481507
pub enum FluxFn {
482508
/// Quadratic flux function
483509
GRADIENT = 1,
@@ -490,6 +516,7 @@ pub enum FluxFn {
490516
/// topk function ordering
491517
#[repr(u32)]
492518
#[derive(Clone, Copy, Debug, PartialEq)]
519+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
493520
pub enum TopkFn {
494521
/// Top k min values
495522
MIN = 1,
@@ -502,6 +529,7 @@ pub enum TopkFn {
502529
/// Iterative Deconvolution Algorithm
503530
#[repr(u32)]
504531
#[derive(Clone, Copy, Debug, PartialEq)]
532+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
505533
pub enum IterativeDeconvAlgo {
506534
/// Land-Weber Algorithm
507535
LANDWEBER = 1,
@@ -514,6 +542,7 @@ pub enum IterativeDeconvAlgo {
514542
/// Inverse Deconvolution Algorithm
515543
#[repr(u32)]
516544
#[derive(Clone, Copy, Debug, PartialEq)]
545+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
517546
pub enum InverseDeconvAlgo {
518547
/// Tikhonov algorithm
519548
TIKHONOV = 1,
@@ -524,6 +553,7 @@ pub enum InverseDeconvAlgo {
524553
/// Gradient mode for convolution
525554
#[repr(u32)]
526555
#[derive(Clone, Copy, Debug, PartialEq)]
556+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
527557
pub enum ConvGradientType {
528558
/// Filter Gradient
529559
FILTER = 1,
@@ -538,6 +568,7 @@ pub enum ConvGradientType {
538568
/// Gradient mode for convolution
539569
#[repr(u32)]
540570
#[derive(Clone, Copy, Debug, PartialEq)]
571+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
541572
pub enum VarianceBias {
542573
/// Sample variance
543574
SAMPLE = 1,
@@ -550,9 +581,29 @@ pub enum VarianceBias {
550581
/// Gradient mode for convolution
551582
#[repr(u32)]
552583
#[derive(Clone, Copy, Debug, PartialEq)]
584+
#[cfg_attr(feature = "afserde", derive(Serialize, Deserialize))]
553585
pub enum CublasMathMode {
554586
/// To indicate use of Tensor Cores on CUDA capable GPUs
555587
TENSOR_OP = 1,
556588
/// Default i.e. tensor core operations will be avoided by the library
557589
DEFAULT = 0,
558590
}
591+
592+
#[cfg(test)]
593+
mod tests {
594+
#[cfg(feature = "afserde")]
595+
#[test]
596+
fn test_enum_serde() {
597+
use super::AfError;
598+
599+
let err_code = AfError::ERR_NO_MEM;
600+
let serd = match serde_json::to_string(&err_code) {
601+
Ok(serialized_str) => serialized_str,
602+
Err(e) => e.to_string(),
603+
};
604+
assert_eq!(serd, "\"ERR_NO_MEM\"");
605+
606+
let deserd: AfError = serde_json::from_str(&serd).unwrap();
607+
assert_eq!(deserd, AfError::ERR_NO_MEM);
608+
}
609+
}

0 commit comments

Comments
 (0)