Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl byte_size on Scalar::Type #499

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
}
}
}