Skip to content

Commit 2be08ec

Browse files
authored
Merge pull request #7 from LaurenzV/no-unsafe
Forbid unsafe code
2 parents 940cc74 + 4bd0649 commit 2be08ec

File tree

6 files changed

+14
-34
lines changed

6 files changed

+14
-34
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ exclude = ["benches/", "tests/"]
1313

1414
[dependencies]
1515
bitflags = "2.4.1"
16-
bytemuck = { version = "1.5", features = ["extern_crate_alloc"] }
16+
bytemuck = { version = "1.5", features = ["extern_crate_alloc", "derive"] }
1717
core_maths = "0.1.0" # only for no_std builds
1818
smallvec = "1.6"
1919
unicode-bidi-mirroring = "0.3.0"

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,8 @@ All of this is a lot of work, so contributions are more than welcome.
9999

100100
## Safety
101101

102-
The library is completely safe.
103-
104-
We do have one `unsafe` to cast between two POD structures, which is perfectly safe.
105-
But except that, there are no `unsafe` in this library and in most of its dependencies
106-
(excluding `bytemuck`).
102+
Unsafe code is forbidden by a `#![forbid(unsafe_code)]` attribute in the root
103+
of the library.
107104

108105
## Alternatives
109106

src/hb/buffer.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use alloc::{string::String, vec::Vec};
2+
use bytemuck::{Pod, Zeroable};
23
use core::cmp::min;
34
use core::convert::TryFrom;
45
use ttf_parser::GlyphId;
@@ -105,7 +106,7 @@ pub mod glyph_flag {
105106
///
106107
/// All positions are relative to the current point.
107108
#[repr(C)]
108-
#[derive(Clone, Copy, Default, Debug)]
109+
#[derive(Clone, Copy, Default, Debug, Zeroable, Pod)]
109110
pub struct GlyphPosition {
110111
/// How much the line advances after drawing this glyph when setting text in
111112
/// horizontal direction.
@@ -122,9 +123,6 @@ pub struct GlyphPosition {
122123
pub(crate) var: u32,
123124
}
124125

125-
unsafe impl bytemuck::Zeroable for GlyphPosition {}
126-
unsafe impl bytemuck::Pod for GlyphPosition {}
127-
128126
impl GlyphPosition {
129127
#[inline]
130128
pub(crate) fn attach_chain(&self) -> i16 {
@@ -157,7 +155,7 @@ impl GlyphPosition {
157155

158156
/// A glyph info.
159157
#[repr(C)]
160-
#[derive(Clone, Copy, Default, Debug)]
158+
#[derive(Clone, Copy, Default, Debug, Zeroable, Pod)]
161159
pub struct hb_glyph_info_t {
162160
// NOTE: Stores a Unicode codepoint before shaping and a glyph ID after.
163161
// Just like harfbuzz, we are using the same variable for two purposes.
@@ -175,9 +173,6 @@ pub struct hb_glyph_info_t {
175173
pub(crate) var2: u32,
176174
}
177175

178-
unsafe impl bytemuck::Zeroable for hb_glyph_info_t {}
179-
unsafe impl bytemuck::Pod for hb_glyph_info_t {}
180-
181176
impl hb_glyph_info_t {
182177
/// Indicates that if input text is broken at the beginning of the cluster this glyph
183178
/// is part of, then both sides need to be re-shaped, as the result might be different.

src/hb/face.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use bytemuck::{Pod, Zeroable};
12
#[cfg(not(feature = "std"))]
23
use core_maths::CoreFloat;
34

@@ -351,14 +352,11 @@ impl<'a> hb_font_t<'a> {
351352
}
352353
}
353354

354-
#[derive(Clone, Copy, Default)]
355+
#[derive(Clone, Copy, Default, Zeroable, Pod)]
355356
#[repr(C)]
356357
pub struct hb_glyph_extents_t {
357358
pub x_bearing: i32,
358359
pub y_bearing: i32,
359360
pub width: i32,
360361
pub height: i32,
361362
}
362-
363-
unsafe impl bytemuck::Zeroable for hb_glyph_extents_t {}
364-
unsafe impl bytemuck::Pod for hb_glyph_extents_t {}

src/hb/shape_wasm.rs

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use alloc::{borrow::ToOwned, ffi::CString, format};
2+
use bytemuck::{Pod, Zeroable};
23
use core::ffi::CStr;
34
use ttf_parser::{GlyphId, Tag};
45
use wasmi::{self, AsContextMut, Caller, Config, Engine, Linker, Module, Store};
@@ -224,7 +225,7 @@ enum PointType {
224225
}
225226

226227
#[repr(C)]
227-
#[derive(Clone, Copy, Debug)]
228+
#[derive(Clone, Copy, Debug, Zeroable, Pod)]
228229
struct OutlinePoint {
229230
x: f32,
230231
y: f32,
@@ -236,9 +237,6 @@ impl OutlinePoint {
236237
}
237238
}
238239

239-
unsafe impl bytemuck::Zeroable for OutlinePoint {}
240-
unsafe impl bytemuck::Pod for OutlinePoint {}
241-
242240
#[derive(Default)]
243241
struct GlyphOutline {
244242
points: alloc::vec::Vec<OutlinePoint>,
@@ -279,17 +277,14 @@ impl ttf_parser::OutlineBuilder for GlyphOutline {
279277
}
280278

281279
#[repr(C)]
282-
#[derive(Debug, Clone, Copy)]
280+
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
283281
struct CGlyphOutline {
284282
n_points: u32,
285283
points: u32, // pointer
286284
n_contours: u32,
287285
contours: u32, // pointer
288286
}
289287

290-
unsafe impl bytemuck::Zeroable for CGlyphOutline {}
291-
unsafe impl bytemuck::Pod for CGlyphOutline {}
292-
293288
// fn font_copy_glyph_outline(font: u32, glyph: u32, outline: *mut CGlyphOutline) -> bool;
294289
// Copies the outline of the given glyph ID, at current scale and variation settings, into the outline structure provided.
295290
fn font_copy_glyph_outline(
@@ -352,16 +347,13 @@ fn font_copy_glyph_outline(
352347
}
353348

354349
#[repr(C)]
355-
#[derive(Debug, Clone, Copy)]
350+
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
356351
struct Blob {
357352
// Length of the blob in bytes
358353
length: u32,
359354
data: u32, // pointer
360355
}
361356

362-
unsafe impl bytemuck::Zeroable for Blob {}
363-
unsafe impl bytemuck::Pod for Blob {}
364-
365357
// fn face_copy_table(font: u32, tag: u32, blob: *mut Blob) -> bool;
366358
// Copies the binary data in the OpenType table referenced by tag into the supplied blob structure.
367359
fn face_copy_table(mut caller: Caller<'_, ShapingData>, _font: u32, tag: u32, blob: u32) -> u32 {
@@ -462,16 +454,13 @@ fn buffer_copy_contents(mut caller: Caller<'_, ShapingData>, _buffer: u32, cbuff
462454
}
463455

464456
#[repr(C)]
465-
#[derive(Debug, Clone, Copy)]
457+
#[derive(Debug, Clone, Copy, Zeroable, Pod)]
466458
struct CBufferContents {
467459
length: u32,
468460
info: u32, // pointer
469461
position: u32, // pointer
470462
}
471463

472-
unsafe impl bytemuck::Zeroable for CBufferContents {}
473-
unsafe impl bytemuck::Pod for CBufferContents {}
474-
475464
// fn buffer_set_contents(buffer: u32, cbuffer: &CBufferContents) -> bool;
476465
// Copy the buffer_contents structure back into the host shaping engine's buffer. This should typically be called at the end of shaping.
477466
fn buffer_set_contents(mut caller: Caller<'_, ShapingData>, _buffer: u32, cbuffer: u32) -> u32 {

src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ A complete [harfbuzz](https://github.com/harfbuzz/harfbuzz) shaping algorithm po
33
*/
44

55
#![no_std]
6+
#![forbid(unsafe_code)]
67
#![warn(missing_docs)]
78

89
#[cfg(feature = "std")]

0 commit comments

Comments
 (0)