pub struct AsNumber<T: Into<String>>(pub T);Expand description
A generic newtype wrapper that converts any T: Into<String> directly to
a DynamoDB N (number) attribute value without parsing.
Use AsNumber when you already have a correctly-formatted numeric string
and want to pass it to DynamoDB as-is — for example, a high-precision
decimal from an external API, a value from a financial system that must
not be rounded through an f64, or a number string received from another
DynamoDB client. T can be &str, String, Cow<str>,
or any other type that implements Into<String>.
AsNumber implements IntoAttributeValue (producing
AttributeValue::N) and
IntoTypedAttributeValue<NumberAttribute>,
so it can be used anywhere a NumberAttribute value is expected (e.g. in
has_attributes! blocks or expression builders).
AsNumber<T> also implements Deref<Target = T>,
so you can use it anywhere a &T is accepted.
§DynamoDB number constraints
DynamoDB numbers can be positive, negative, or zero, with up to 38 digits of precision (exceeding this causes a runtime error). The supported ranges are:
- Positive:
1E-130to9.9999999999999999999999999999999999999E+125 - Negative:
-9.9999999999999999999999999999999999999E+125to-1E-130
Leading and trailing zeroes are trimmed by DynamoDB. Numbers are transmitted as strings over the wire but treated as numeric types for mathematical operations.
Warning: No validation is performed on the wrapped value. An invalid number string (e.g.
"not-a-number") will be accepted byAsNumberbut rejected by DynamoDB at runtime.
§Examples
Basic usage — converting a pre-formatted decimal string to AttributeValue::N:
use dynamodb_facade::{AsNumber, AttributeValue, IntoAttributeValue};
// A high-precision decimal that would lose precision as f64
let price = AsNumber("12345678.90123456789099");
let av = price.into_attribute_value();
assert_eq!(av, AttributeValue::N("12345678.90123456789099".to_owned()));Using AsNumber where a NumberAttribute value
is required — the type system accepts it just like any numeric primitive:
use dynamodb_facade::{AsNumber, IntoTypedAttributeValue, NumberAttribute};
fn store_score<V: IntoTypedAttributeValue<NumberAttribute>>(_v: V) {}
// AsNumber satisfies the NumberAttribute bound
store_score(AsNumber("99.5"));
// So do ordinary numeric primitives
store_score(42);Tuple Fields§
§0: TTrait Implementations§
Source§impl<T: Into<String>> IntoAttributeValue for AsNumber<T>
impl<T: Into<String>> IntoAttributeValue for AsNumber<T>
Source§fn into_attribute_value(self) -> AttributeValue
fn into_attribute_value(self) -> AttributeValue
self into a DynamoDB AttributeValue.Auto Trait Implementations§
impl<T> Freeze for AsNumber<T>where
T: Freeze,
impl<T> RefUnwindSafe for AsNumber<T>where
T: RefUnwindSafe,
impl<T> Send for AsNumber<T>where
T: Send,
impl<T> Sync for AsNumber<T>where
T: Sync,
impl<T> Unpin for AsNumber<T>where
T: Unpin,
impl<T> UnsafeUnpin for AsNumber<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for AsNumber<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more