Skip to content
Open
Show file tree
Hide file tree
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
19 changes: 12 additions & 7 deletions src/hb/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::hb::unsafe_vec::UnsafeVec;
use crate::U32Set;
use alloc::{string::String, vec::Vec};
use alloc::string::String;
use core::cmp::min;
use core::convert::TryFrom;
use read_fonts::types::{GlyphId, GlyphId16};
Expand Down Expand Up @@ -420,8 +421,8 @@ pub struct hb_buffer_t {
pub len: usize,
pub out_len: usize,

pub info: Vec<GlyphInfo>,
pub pos: Vec<GlyphPosition>,
pub info: UnsafeVec<GlyphInfo>,
pub pos: UnsafeVec<GlyphPosition>,

// Text before / after the main buffer contents.
// Always in Unicode, and ordered outward.
Expand Down Expand Up @@ -472,8 +473,8 @@ impl hb_buffer_t {
idx: 0,
len: 0,
out_len: 0,
info: Vec::new(),
pos: Vec::new(),
info: UnsafeVec::new(),
pos: UnsafeVec::new(),
have_separate_output: false,
allocated_var_bits: 0,
serial: 0,
Expand Down Expand Up @@ -770,8 +771,12 @@ impl hb_buffer_t {

if self.have_separate_output {
// Swap info and pos buffers.
let info: Vec<GlyphPosition> = bytemuck::cast_vec(core::mem::take(&mut self.info));
let pos: Vec<GlyphInfo> = bytemuck::cast_vec(core::mem::take(&mut self.pos));
let info: UnsafeVec<GlyphPosition> = UnsafeVec {
inner: bytemuck::cast_vec(core::mem::take(&mut self.info)),
};
let pos: UnsafeVec<GlyphInfo> = UnsafeVec {
inner: bytemuck::cast_vec(core::mem::take(&mut self.pos)),
};
self.pos = info;
self.info = pos;
self.have_separate_output = false;
Expand Down
1 change: 1 addition & 0 deletions src/hb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ mod text_parser;
#[rustfmt::skip]
mod ucd_table;
mod unicode;
mod unsafe_vec;

use read_fonts::types::Tag as hb_tag_t;

Expand Down
203 changes: 203 additions & 0 deletions src/hb/unsafe_vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
use std::fmt;
use std::ops::{
Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo,
RangeToInclusive,
};

#[repr(transparent)]
pub struct UnsafeVec<T> {
pub inner: Vec<T>,
}

impl<T> UnsafeVec<T> {
#[inline]
pub fn new() -> Self {
Self { inner: Vec::new() }
}

#[inline]
pub fn with_capacity(cap: usize) -> Self {
Self {
inner: Vec::with_capacity(cap),
}
}

#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}

#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}

#[inline]
pub fn push(&mut self, value: T) {
self.inner.push(value);
}

#[inline]
pub fn as_ptr(&self) -> *const T {
self.inner.as_ptr()
}

#[inline]
pub fn as_mut_ptr(&mut self) -> *mut T {
self.inner.as_mut_ptr()
}
}

impl<T> Default for UnsafeVec<T> {
#[inline]
fn default() -> Self {
Self::new()
}
}

impl<T: fmt::Debug> fmt::Debug for UnsafeVec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}

/// Expose full Vec<T> API (resize/truncate/extend/etc.)
impl<T> Deref for UnsafeVec<T> {
type Target = Vec<T>;
#[inline]
fn deref(&self) -> &Vec<T> {
&self.inner
}
}
impl<T> DerefMut for UnsafeVec<T> {
#[inline]
fn deref_mut(&mut self) -> &mut Vec<T> {
&mut self.inner
}
}

/// Unchecked single-element indexing (UB if out of bounds)
impl<T> Index<usize> for UnsafeVec<T> {
type Output = T;
#[inline]
fn index(&self, i: usize) -> &T {
unsafe { self.inner.get_unchecked(i) }
}
}
impl<T> IndexMut<usize> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, i: usize) -> &mut T {
unsafe { self.inner.get_unchecked_mut(i) }
}
}

/// Safe range indexing
impl<T> Index<Range<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: Range<usize>) -> &[T] {
&self.inner[r]
}
}
impl<T> IndexMut<Range<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: Range<usize>) -> &mut [T] {
&mut self.inner[r]
}
}

impl<T> Index<RangeFrom<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: RangeFrom<usize>) -> &[T] {
&self.inner[r]
}
}
impl<T> IndexMut<RangeFrom<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: RangeFrom<usize>) -> &mut [T] {
&mut self.inner[r]
}
}

impl<T> Index<RangeTo<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: RangeTo<usize>) -> &[T] {
&self.inner[r]
}
}
impl<T> IndexMut<RangeTo<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: RangeTo<usize>) -> &mut [T] {
&mut self.inner[r]
}
}

impl<T> Index<RangeFull> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, _: RangeFull) -> &[T] {
&self.inner
}
}
impl<T> IndexMut<RangeFull> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, _: RangeFull) -> &mut [T] {
&mut self.inner
}
}

impl<T> Index<RangeInclusive<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: RangeInclusive<usize>) -> &[T] {
&self.inner[r]
}
}
impl<T> IndexMut<RangeInclusive<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: RangeInclusive<usize>) -> &mut [T] {
&mut self.inner[r]
}
}

impl<T> Index<RangeToInclusive<usize>> for UnsafeVec<T> {
type Output = [T];
#[inline]
fn index(&self, r: RangeToInclusive<usize>) -> &[T] {
&self.inner[r]
}
}
impl<T> IndexMut<RangeToInclusive<usize>> for UnsafeVec<T> {
#[inline]
fn index_mut(&mut self, r: RangeToInclusive<usize>) -> &mut [T] {
&mut self.inner[r]
}
}

/// Iteration support: `for x in &v`, `for x in &mut v`, `for x in v`
impl<'a, T> IntoIterator for &'a UnsafeVec<T> {
type Item = &'a T;
type IntoIter = std::slice::Iter<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.inner.iter()
}
}
impl<'a, T> IntoIterator for &'a mut UnsafeVec<T> {
type Item = &'a mut T;
type IntoIter = std::slice::IterMut<'a, T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.inner.iter_mut()
}
}
impl<T> IntoIterator for UnsafeVec<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
self.inner.into_iter()
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A complete [harfbuzz](https://github.com/harfbuzz/harfbuzz) shaping algorithm po
#![cfg_attr(not(feature = "std"), no_std)]
// Forbidding unsafe code only applies to the lib
// examples continue to use it, so this cannot be placed into Cargo.toml
#![forbid(unsafe_code)]
//#![forbid(unsafe_code)]
#![warn(missing_docs)]

extern crate alloc;
Expand Down
Loading