Skip to main content

AsNumber

Struct AsNumber 

Source
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-130 to 9.9999999999999999999999999999999999999E+125
  • Negative: -9.9999999999999999999999999999999999999E+125 to -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 by AsNumber but 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: T

Trait Implementations§

Source§

impl<T: Into<String>> Deref for AsNumber<T>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<T: Into<String>> IntoAttributeValue for AsNumber<T>

Source§

fn into_attribute_value(self) -> AttributeValue

Converts 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> IntoTypedAttributeValue<NumberAttribute> for T
where T: IntoNumberAttributeValue + IntoAttributeValue,

Source§

impl<T> Parsable for T
where T: Deref, <T as Deref>::Target: Parsable,