Skip to main content

kobo_core/rendering/
common.rs

1//! Shared rendering state and constants.
2
3use std::sync::atomic::{AtomicBool, Ordering};
4
5static IS_RTL: AtomicBool = AtomicBool::new(false);
6
7pub fn set_rtl(rtl: bool) {
8    IS_RTL.store(rtl, Ordering::Relaxed);
9}
10
11pub fn is_rtl() -> bool {
12    IS_RTL.load(Ordering::Relaxed)
13}
14
15pub const BODY_PX: f32 = 36.0;
16
17/// Reinterpret a slice of any type as a shared `&[u8]`.
18///
19/// SAFETY: the slice covers exactly `size_of_val(buf)` bytes starting at the
20/// same address. `u8` has no alignment requirement, so any `T` is valid.
21pub fn slice_as_bytes<T>(buf: &[T]) -> &[u8] {
22    unsafe { std::slice::from_raw_parts(buf.as_ptr() as *const u8, std::mem::size_of_val(buf)) }
23}
24
25/// Reinterpret a mutable slice of any type as a `&mut [u8]`.
26///
27/// SAFETY: same as [`slice_as_bytes`], but with an exclusive `&mut` borrow.
28pub fn slice_as_bytes_mut<T>(buf: &mut [T]) -> &mut [u8] {
29    unsafe {
30        std::slice::from_raw_parts_mut(buf.as_mut_ptr() as *mut u8, std::mem::size_of_val(buf))
31    }
32}