1 /// Describes the arithmetic operation in an atomic memory read-modify-write operation. 2 use core::fmt::{self, Display, Formatter}; 3 use core::str::FromStr; 4 #[cfg(feature = "enable-serde")] 5 use serde::{Deserialize, Serialize}; 6 7 #[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)] 8 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] 9 /// Describes the arithmetic operation in an atomic memory read-modify-write operation. 10 pub enum AtomicRmwOp { 11 /// Add 12 Add, 13 /// Sub 14 Sub, 15 /// And 16 And, 17 /// Nand 18 Nand, 19 /// Or 20 Or, 21 /// Xor 22 Xor, 23 /// Exchange 24 Xchg, 25 /// Unsigned min 26 Umin, 27 /// Unsigned max 28 Umax, 29 /// Signed min 30 Smin, 31 /// Signed max 32 Smax, 33 } 34 35 impl Display for AtomicRmwOp { fmt(&self, f: &mut Formatter) -> fmt::Result36 fn fmt(&self, f: &mut Formatter) -> fmt::Result { 37 let s = match self { 38 AtomicRmwOp::Add => "add", 39 AtomicRmwOp::Sub => "sub", 40 AtomicRmwOp::And => "and", 41 AtomicRmwOp::Nand => "nand", 42 AtomicRmwOp::Or => "or", 43 AtomicRmwOp::Xor => "xor", 44 AtomicRmwOp::Xchg => "xchg", 45 AtomicRmwOp::Umin => "umin", 46 AtomicRmwOp::Umax => "umax", 47 AtomicRmwOp::Smin => "smin", 48 AtomicRmwOp::Smax => "smax", 49 }; 50 f.write_str(s) 51 } 52 } 53 54 impl FromStr for AtomicRmwOp { 55 type Err = (); from_str(s: &str) -> Result<Self, Self::Err>56 fn from_str(s: &str) -> Result<Self, Self::Err> { 57 match s { 58 "add" => Ok(AtomicRmwOp::Add), 59 "sub" => Ok(AtomicRmwOp::Sub), 60 "and" => Ok(AtomicRmwOp::And), 61 "nand" => Ok(AtomicRmwOp::Nand), 62 "or" => Ok(AtomicRmwOp::Or), 63 "xor" => Ok(AtomicRmwOp::Xor), 64 "xchg" => Ok(AtomicRmwOp::Xchg), 65 "umin" => Ok(AtomicRmwOp::Umin), 66 "umax" => Ok(AtomicRmwOp::Umax), 67 "smin" => Ok(AtomicRmwOp::Smin), 68 "smax" => Ok(AtomicRmwOp::Smax), 69 _ => Err(()), 70 } 71 } 72 } 73