Skip to content

Commit d90edd1

Browse files
authored
Impl byte_size on Scalar::Type (#499)
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
1 parent ae31f18 commit d90edd1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

mozjs-sys/src/jsimpls.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use crate::jsapi::JSPropertySpec;
1919
use crate::jsapi::JSPropertySpec_Kind;
2020
use crate::jsapi::JSPropertySpec_Name;
2121
use crate::jsapi::JS;
22+
use crate::jsapi::JS::Scalar::Type;
2223
use crate::jsgc::RootKind;
2324
use crate::jsid::VoidId;
2425
use crate::jsval::UndefinedValue;
@@ -574,3 +575,19 @@ impl<T> mozilla::Range<T> {
574575
}
575576
}
576577
}
578+
579+
impl Type {
580+
/// Returns byte size of Type (if possible to determine)
581+
///
582+
/// <https://searchfox.org/mozilla-central/rev/396a6123691f7ab3ffb449dcbe95304af6f9df3c/js/public/ScalarType.h#66>
583+
pub const fn byte_size(&self) -> Option<usize> {
584+
match self {
585+
Type::Int8 | Type::Uint8 | Type::Uint8Clamped => Some(1),
586+
Type::Int16 | Type::Uint16 | Type::Float16 => Some(2),
587+
Type::Int32 | Type::Uint32 | Type::Float32 => Some(4),
588+
Type::Int64 | Type::Float64 | Type::BigInt64 | Type::BigUint64 => Some(8),
589+
Type::Simd128 => Some(16),
590+
Type::MaxTypedArrayViewType => None,
591+
}
592+
}
593+
}

0 commit comments

Comments
 (0)