Skip to content

Commit

Permalink
Impl byte_size on Scalar::Type (#499)
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <[email protected]>
  • Loading branch information
sagudev authored Aug 19, 2024
1 parent ae31f18 commit d90edd1
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions mozjs-sys/src/jsimpls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::jsapi::JSPropertySpec;
use crate::jsapi::JSPropertySpec_Kind;
use crate::jsapi::JSPropertySpec_Name;
use crate::jsapi::JS;
use crate::jsapi::JS::Scalar::Type;
use crate::jsgc::RootKind;
use crate::jsid::VoidId;
use crate::jsval::UndefinedValue;
Expand Down Expand Up @@ -574,3 +575,19 @@ impl<T> mozilla::Range<T> {
}
}
}

impl Type {
/// Returns byte size of Type (if possible to determine)
///
/// <https://searchfox.org/mozilla-central/rev/396a6123691f7ab3ffb449dcbe95304af6f9df3c/js/public/ScalarType.h#66>
pub const fn byte_size(&self) -> Option<usize> {
match self {
Type::Int8 | Type::Uint8 | Type::Uint8Clamped => Some(1),
Type::Int16 | Type::Uint16 | Type::Float16 => Some(2),
Type::Int32 | Type::Uint32 | Type::Float32 => Some(4),
Type::Int64 | Type::Float64 | Type::BigInt64 | Type::BigUint64 => Some(8),
Type::Simd128 => Some(16),
Type::MaxTypedArrayViewType => None,
}
}
}

0 comments on commit d90edd1

Please sign in to comment.