You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mg...@apache.org on 2022/02/02 19:37:11 UTC
[avro] branch master updated: AVRO-3331: Add TryFrom owned Decimal (#1502)
This is an automated email from the ASF dual-hosted git repository.
mgrigorov pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git
The following commit(s) were added to refs/heads/master by this push:
new 8ce603b AVRO-3331: Add TryFrom owned Decimal (#1502)
8ce603b is described below
commit 8ce603b6317638847fa547f0d2c587cd019903db
Author: Martin Grigorov <ma...@users.noreply.github.com>
AuthorDate: Wed Feb 2 21:35:53 2022 +0200
AVRO-3331: Add TryFrom owned Decimal (#1502)
Add more docs and simple unit tests
Signed-off-by: Martin Tzvetanov Grigorov <mg...@apache.org>
---
lang/rust/src/decimal.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)
diff --git a/lang/rust/src/decimal.rs b/lang/rust/src/decimal.rs
index 8a4196d..cfd876b 100644
--- a/lang/rust/src/decimal.rs
+++ b/lang/rust/src/decimal.rs
@@ -55,6 +55,15 @@ impl Decimal {
}
}
+/// Gets the internal byte array representation of a referenced decimal.
+/// Usage:
+/// ```
+/// use apache_avro::Decimal;
+/// use std::convert::TryFrom;
+///
+/// let decimal = Decimal::from(vec![1, 24]);
+/// let maybe_bytes = <Vec<u8>>::try_from(&decimal);
+/// ```
impl std::convert::TryFrom<&Decimal> for Vec<u8> {
type Error = Error;
@@ -63,6 +72,23 @@ impl std::convert::TryFrom<&Decimal> for Vec<u8> {
}
}
+/// Gets the internal byte array representation of an owned decimal.
+/// Usage:
+/// ```
+/// use apache_avro::Decimal;
+/// use std::convert::TryFrom;
+///
+/// let decimal = Decimal::from(vec![1, 24]);
+/// let maybe_bytes = <Vec<u8>>::try_from(decimal);
+/// ```
+impl std::convert::TryFrom<Decimal> for Vec<u8> {
+ type Error = Error;
+
+ fn try_from(decimal: Decimal) -> Result<Self, Self::Error> {
+ decimal.to_vec()
+ }
+}
+
impl<T: AsRef<[u8]>> From<T> for Decimal {
fn from(bytes: T) -> Self {
let bytes_ref = bytes.as_ref();
@@ -72,3 +98,27 @@ impl<T: AsRef<[u8]>> From<T> for Decimal {
}
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::convert::TryFrom;
+
+ #[test]
+ fn test_decimal_from_bytes_from_ref_decimal() {
+ let input = vec![1, 24];
+ let d = Decimal::from(&input);
+
+ let output = <Vec<u8>>::try_from(&d).unwrap();
+ assert_eq!(output, input);
+ }
+
+ #[test]
+ fn test_decimal_from_bytes_from_owned_decimal() {
+ let input = vec![1, 24];
+ let d = Decimal::from(&input);
+
+ let output = <Vec<u8>>::try_from(d).unwrap();
+ assert_eq!(output, input);
+ }
+}