pub struct SafeInteger<T: SafeIntegerPrimitive>(/* private fields */);Expand description
A u64 or i64 that crosses the JS boundary as a number instead of a
BigInt.
Many JS APIs type integer counts as number. A plain u64 in a binding
signature is a BigInt at the boundary, so SafeInteger<u64> is used
instead to keep the Rust integer type while passing a number. In
TypeScript it is number.
use wasm_bindgen::SafeInteger;
#[wasm_bindgen]
extern "C" {
type QueueMetrics;
#[wasm_bindgen(method, getter, js_name = backlogCount)]
fn backlog_count(this: &QueueMetrics) -> SafeInteger<u64>;
}
let count = metrics.backlog_count();
if count > 10_000 { /* ... */ }
let next: u64 = (count + 1).into();It derefs to the inner integer, so its methods are available directly, and compares and does arithmetic with both itself and the inner type.
The value is expected to be within ±(2^53 - 1) (Number.MAX_SAFE_INTEGER)
and, for u64, non-negative, so that it is exactly representable as a JS
number. Like the overflow checks on Rust’s own integers, this is only
checked with debug assertions enabled: constructing a value out of range,
arithmetic leaving the range, or receiving a non-integer or out-of-range
number from JS then panics. Without debug assertions an out-of-range
value converts lossily, as an as cast would. SafeInteger::new and
the TryFrom<JsValue> conversion always check.
SafeInteger is supported in every position its conversion is reached
through: arguments and returns, catch results, Option, Vec and
Box<[_]>, borrowed slices passed into Rust, closure arguments, async
import results and generic positions on experimental_generic_mono
imports. A borrowed slice &[SafeInteger<_>] cannot be passed out to JS,
since the elements have to be converted; pass a Vec or Box<[_]>
instead.
Implementations§
Source§impl<T: SafeIntegerPrimitive> SafeInteger<T>
impl<T: SafeIntegerPrimitive> SafeInteger<T>
Trait Implementations§
Source§impl<T: SafeIntegerPrimitive + Add<Output = T>> Add for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Add<Output = T>> Add for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Add<Output = T>> Add<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Add<Output = T>> Add<T> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Add<Output = T>> AddAssign for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Add<Output = T>> AddAssign for SafeInteger<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<T: SafeIntegerPrimitive + Add<Output = T>> AddAssign<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Add<Output = T>> AddAssign<T> for SafeInteger<T>
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+= operation. Read moreSource§impl<T: Clone + SafeIntegerPrimitive> Clone for SafeInteger<T>
impl<T: Clone + SafeIntegerPrimitive> Clone for SafeInteger<T>
impl<T: Copy + SafeIntegerPrimitive> Copy for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Debug> Debug for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Debug> Debug for SafeInteger<T>
Source§impl<T: Default + SafeIntegerPrimitive> Default for SafeInteger<T>
impl<T: Default + SafeIntegerPrimitive> Default for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> Deref for SafeInteger<T>
impl<T: SafeIntegerPrimitive> Deref for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Display> Display for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Display> Display for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Div<Output = T>> Div for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Div<Output = T>> Div for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Div<Output = T>> Div<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Div<Output = T>> Div<T> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Div<Output = T>> DivAssign for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Div<Output = T>> DivAssign for SafeInteger<T>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl<T: SafeIntegerPrimitive + Div<Output = T>> DivAssign<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Div<Output = T>> DivAssign<T> for SafeInteger<T>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/= operation. Read moreimpl<T: Eq + SafeIntegerPrimitive> Eq for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> ErasableGeneric for SafeInteger<T>
impl<T: SafeIntegerPrimitive> ErasableGeneric for SafeInteger<T>
Source§type Repr = SafeInteger<T>
type Repr = SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> From<SafeInteger<T>> for JsValue
impl<T: SafeIntegerPrimitive> From<SafeInteger<T>> for JsValue
Source§fn from(n: SafeInteger<T>) -> JsValue
fn from(n: SafeInteger<T>) -> JsValue
Source§impl<T: SafeIntegerPrimitive> From<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive> From<T> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> FromWasmAbi for SafeInteger<T>
impl<T: SafeIntegerPrimitive> FromWasmAbi for SafeInteger<T>
Source§impl<T: Hash + SafeIntegerPrimitive> Hash for SafeInteger<T>
impl<T: Hash + SafeIntegerPrimitive> Hash for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> IntoWasmAbi for SafeInteger<T>
impl<T: SafeIntegerPrimitive> IntoWasmAbi for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> IntoWasmAbi for &SafeInteger<T>
impl<T: SafeIntegerPrimitive> IntoWasmAbi for &SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> MaybeFromWasmAbi for SafeInteger<T>
impl<T: SafeIntegerPrimitive> MaybeFromWasmAbi for SafeInteger<T>
Source§unsafe fn from_maybe_abi(js: Self::MaybeAbi) -> Self
unsafe fn from_maybe_abi(js: Self::MaybeAbi) -> Self
Source§impl<T: SafeIntegerPrimitive> MaybeIntoWasmAbi for SafeInteger<T>
impl<T: SafeIntegerPrimitive> MaybeIntoWasmAbi for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> MaybeIntoWasmAbi for &SafeInteger<T>
impl<T: SafeIntegerPrimitive> MaybeIntoWasmAbi for &SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Mul<Output = T>> Mul for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Mul<Output = T>> Mul for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Mul<Output = T>> Mul<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Mul<Output = T>> Mul<T> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Mul<Output = T>> MulAssign for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Mul<Output = T>> MulAssign for SafeInteger<T>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<T: SafeIntegerPrimitive + Mul<Output = T>> MulAssign<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Mul<Output = T>> MulAssign<T> for SafeInteger<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*= operation. Read moreSource§impl Neg for SafeInteger<i64>
impl Neg for SafeInteger<i64>
Source§impl<T: SafeIntegerPrimitive> NullableFromWasmAbi for SafeInteger<T>
impl<T: SafeIntegerPrimitive> NullableFromWasmAbi for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> NullableIntoWasmAbi for SafeInteger<T>
impl<T: SafeIntegerPrimitive> NullableIntoWasmAbi for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> NullableIntoWasmAbi for &SafeInteger<T>
impl<T: SafeIntegerPrimitive> NullableIntoWasmAbi for &SafeInteger<T>
Source§impl<T: Ord + SafeIntegerPrimitive> Ord for SafeInteger<T>
impl<T: Ord + SafeIntegerPrimitive> Ord for SafeInteger<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: PartialEq + SafeIntegerPrimitive> PartialEq for SafeInteger<T>
impl<T: PartialEq + SafeIntegerPrimitive> PartialEq for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> PartialEq<SafeInteger<T>> for JsValue
impl<T: SafeIntegerPrimitive> PartialEq<SafeInteger<T>> for JsValue
Source§impl<T: SafeIntegerPrimitive> PartialEq<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive> PartialEq<T> for SafeInteger<T>
Source§impl<T: PartialOrd + SafeIntegerPrimitive> PartialOrd for SafeInteger<T>
impl<T: PartialOrd + SafeIntegerPrimitive> PartialOrd for SafeInteger<T>
Source§impl PartialOrd<SafeInteger<i64>> for i64
impl PartialOrd<SafeInteger<i64>> for i64
Source§impl PartialOrd<SafeInteger<u64>> for u64
impl PartialOrd<SafeInteger<u64>> for u64
Source§impl<T: SafeIntegerPrimitive> PartialOrd<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive> PartialOrd<T> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> Promising for SafeInteger<T>
impl<T: SafeIntegerPrimitive> Promising for SafeInteger<T>
Source§type Resolution = SafeInteger<T>
type Resolution = SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Rem<Output = T>> Rem for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Rem<Output = T>> Rem for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Rem<Output = T>> Rem<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Rem<Output = T>> Rem<T> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Rem<Output = T>> RemAssign for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Rem<Output = T>> RemAssign for SafeInteger<T>
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%= operation. Read moreSource§impl<T: SafeIntegerPrimitive + Rem<Output = T>> RemAssign<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Rem<Output = T>> RemAssign<T> for SafeInteger<T>
Source§fn rem_assign(&mut self, rhs: T)
fn rem_assign(&mut self, rhs: T)
%= operation. Read moreimpl<T: PartialEq + SafeIntegerPrimitive> StructuralPartialEq for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Sub<Output = T>> Sub for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Sub<Output = T>> Sub for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Sub<Output = T>> Sub<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Sub<Output = T>> Sub<T> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive + Sub<Output = T>> SubAssign for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Sub<Output = T>> SubAssign for SafeInteger<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read moreSource§impl<T: SafeIntegerPrimitive + Sub<Output = T>> SubAssign<T> for SafeInteger<T>
impl<T: SafeIntegerPrimitive + Sub<Output = T>> SubAssign<T> for SafeInteger<T>
Source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
-= operation. Read moreSource§impl<T: SafeIntegerPrimitive> TryFrom<&JsValue> for SafeInteger<T>
impl<T: SafeIntegerPrimitive> TryFrom<&JsValue> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> TryFrom<JsValue> for SafeInteger<T>
impl<T: SafeIntegerPrimitive> TryFrom<JsValue> for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> TryFromJsValue for SafeInteger<T>
impl<T: SafeIntegerPrimitive> TryFromJsValue for SafeInteger<T>
Source§fn try_from_js_value_ref(value: &JsValue) -> Option<Self>
fn try_from_js_value_ref(value: &JsValue) -> Option<Self>
Source§const NULLABLE: bool = false
const NULLABLE: bool = false
Self accepts null as a distinct value space, as
Nullable<T> does. An enclosing Option then passes null through
instead of reading it as None, so Option<T> is itself nullable
exactly when T is not. JsValue does not set this: it accepts
null as one value among all of them, not as a distinct space, and
Option<JsValue> reads it as None like the ABI does. This is the
JS-value side of NullableFromWasmAbi, which claims null at the
ABI.impl<T: SafeIntegerPrimitive> UpcastFrom<SafeInteger<T>> for SafeInteger<T>
impl<T: SafeIntegerPrimitive> UpcastFrom<SafeInteger<T>> for f64
impl<T: SafeIntegerPrimitive> UpcastFrom<SafeInteger<T>> for JsOption<f64>
impl<T: SafeIntegerPrimitive> UpcastFrom<SafeInteger<T>> for JsNullable<f64>
impl<T: SafeIntegerPrimitive> UpcastFrom<SafeInteger<T>> for JsValue
impl<T: SafeIntegerPrimitive> UpcastFrom<SafeInteger<T>> for JsOption<JsValue>
impl<T: SafeIntegerPrimitive> UpcastFrom<SafeInteger<T>> for JsNullable<JsValue>
impl<T: SafeIntegerPrimitive, Target: UpcastFrom<SafeInteger<T>>> UpcastFrom<SafeInteger<T>> for Option<Target>
impl<T: SafeIntegerPrimitive, Target: UpcastFrom<SafeInteger<T>>> UpcastFrom<SafeInteger<T>> for Nullable<Target>
Source§impl<T: SafeIntegerPrimitive> VectorFromWasmAbi for SafeInteger<T>
impl<T: SafeIntegerPrimitive> VectorFromWasmAbi for SafeInteger<T>
Source§impl<T: SafeIntegerPrimitive> VectorIntoWasmAbi for SafeInteger<T>
impl<T: SafeIntegerPrimitive> VectorIntoWasmAbi for SafeInteger<T>
Auto Trait Implementations§
impl<T> Freeze for SafeInteger<T>where
T: Freeze,
impl<T> RefUnwindSafe for SafeInteger<T>where
T: RefUnwindSafe,
impl<T> Send for SafeInteger<T>where
T: Send,
impl<T> Sync for SafeInteger<T>where
T: Sync,
impl<T> Unpin for SafeInteger<T>where
T: Unpin,
impl<T> UnsafeUnpin for SafeInteger<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for SafeInteger<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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, A> RefIntoWasmAbi for T
impl<T, A> RefIntoWasmAbi for T
Source§fn describe_ref()
fn describe_ref()
&Self.Source§fn ref_into_abi(&self) -> A
fn ref_into_abi(&self) -> A
&self into Self::Abi.Source§impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
impl<T> ReturnWasmAbi for Twhere
T: IntoWasmAbi,
Source§type Abi = <T as IntoWasmAbi>::Abi
type Abi = <T as IntoWasmAbi>::Abi
IntoWasmAbi::AbiSource§fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
fn return_abi(self) -> <T as ReturnWasmAbi>::Abi
IntoWasmAbi::into_abi, except that it may throw and never
return in the case of Err.