Skip to content

Commit

Permalink
Fix clippy and formatting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-rhdt committed Jan 18, 2020
1 parent f2ca6c3 commit 35d271e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 23 deletions.
2 changes: 0 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


use harfbuzz_rs::{shape, Face, Font, UnicodeBuffer};

// Execute this file from the root directory of this repository.
Expand Down
13 changes: 10 additions & 3 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ pub struct GlyphPosition {
/// how much the line advances after drawing this glyph when setting text in
/// vertical direction.
pub y_advance: Position,
/// how much the glyph moves on the X-axis before drawing it, this should not
/// affect how much the line advances.
/// how much the glyph moves on the X-axis before drawing it, this should
/// not affect how much the line advances.
pub x_offset: Position,
/// how much the glyph moves on the Y-axis before drawing it, this should
/// not affect how much the line advances.
Expand Down Expand Up @@ -81,6 +81,7 @@ impl GlyphPosition {
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct GlyphFlags(pub hb::hb_glyph_flags_t);

#[allow(clippy::trivially_copy_pass_by_ref)]
impl GlyphFlags {
/// If `true`, indicates that if input text is broken at the beginning of
/// the cluster this glyph is part of, then both sides need to be re-shaped,
Expand Down Expand Up @@ -352,7 +353,8 @@ impl<'a> Read for BufferSerializer<'a> {
}
}

/// This type provides an interface to create one of the buffer types from a raw harfbuzz pointer.
/// This type provides an interface to create one of the buffer types from a raw
/// harfbuzz pointer.
#[derive(Debug)]
pub enum TypedBuffer {
/// Contains a `UnicodeBuffer`
Expand All @@ -364,6 +366,11 @@ pub enum TypedBuffer {
impl TypedBuffer {
/// Takes ownership of the raw `hb_buffer_t` object and converts it to are
/// `TypedBuffer`. If no safe conversion is possible returns `None`.
///
/// # Safety
///
/// Marked as unsafe because it acceses a raw pointer. Internally calls
/// `Owned::from_raw` and therefore the same ownership considerations apply.
pub unsafe fn take_from_raw(raw: *mut hb::hb_buffer_t) -> Option<TypedBuffer> {
let generic_buf: Owned<GenericBuffer> = Owned::from_raw(raw);
let content_type = generic_buf.content_type();
Expand Down
36 changes: 33 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ impl Tag {
/// assert_eq!(&tag.to_bytes(), b"abcd");
/// ```
pub const fn to_bytes(self) -> [u8; 4] {
#[allow(clippy::identity_op)]
[
(self.0 >> 24 & 0xff) as u8,
(self.0 >> 16 & 0xff) as u8,
Expand Down Expand Up @@ -224,6 +225,8 @@ impl Script {
/// common functionality for converting from and to the underlying raw harfbuzz
/// pointers that are useful for ffi.
///
/// # Safety
///
/// This trait may only be implemented for structs that are zero-sized and is
/// therefore unsafe to implement.
pub unsafe trait HarfbuzzObject: Sized {
Expand All @@ -248,12 +251,24 @@ pub unsafe trait HarfbuzzObject: Sized {
/// Increases the reference count of the HarfBuzz object.
///
/// Wraps a `hb_TYPE_reference()` call.
///
/// # Safety
///
/// While no undefined behavior can be introduced only by increasing the
/// reference count (I think) this method is still marked unsafe since there
/// should be no need for it to be called from safe code.
unsafe fn reference(&self);

/// Decreases the reference count of the HarfBuzz object and destroys it if
/// the reference count reaches zero.
///
/// Wraps a `hb_TYPE_destroy()` call.
///
/// # Safety
///
/// You always have to call `reference` first before using this method.
/// Otherwise you might accidentally hold on to already destroyed objects
/// and causing UB.
unsafe fn dereference(&self);
}

Expand All @@ -277,6 +292,8 @@ pub struct Shared<T: HarfbuzzObject> {
impl<T: HarfbuzzObject> Shared<T> {
/// Creates a `Shared` from an owned raw harfbuzz pointer.
///
/// # Safety
///
/// Transfers ownership. _Use of the original pointer is now forbidden!_
/// Unsafe because dereferencing a raw pointer is necessary.
pub unsafe fn from_raw_owned(raw: *mut T::Raw) -> Self {
Expand All @@ -295,9 +312,18 @@ impl<T: HarfbuzzObject> Shared<T> {
}

/// Creates a `Shared` by cloning a raw harfbuzz pointer.
///
/// The original pointer can still be safely used but must be released at
/// the end to avoid memory leaks.
///
/// # Safety
///
/// The original pointer can still be safely used but must be released
/// at the end to avoid memory leaks.
/// `raw` must be a valid pointer to the corresponding harfbuzz object or
/// the behavior is undefined.
///
/// Internally this method increases the reference count of `raw` so it is
/// safe to call `hb_destroy_[...]` on `raw` after using `from_raw_ref`.
pub unsafe fn from_raw_ref(raw: *mut T::Raw) -> Self {
let object = T::from_raw(raw);
object.reference();
Expand Down Expand Up @@ -371,11 +397,14 @@ pub struct Owned<T: HarfbuzzObject> {
impl<T: HarfbuzzObject> Owned<T> {
/// Creates a `Owned` safely wrapping a raw harfbuzz pointer.
///
/// # Safety
///
/// This fully transfers ownership. _Use of the original pointer is now
/// forbidden!_ Unsafe because a dereference of a raw pointer is necessary.
///
/// Use this only to wrap freshly created HarfBuzz object that is not
/// shared!
/// shared! Otherwise it is possible to have aliasing mutable references
/// from safe code
pub unsafe fn from_raw(raw: *mut T::Raw) -> Self {
Owned {
object: T::from_raw(raw),
Expand All @@ -400,6 +429,7 @@ impl<T: HarfbuzzObject> Owned<T> {
///
/// Note that `Shared<T>` also implements `From<Owned<T>>` which allows
/// implicit conversions in many functions.
#[allow(clippy::wrong_self_convention)] //< backward compatibility is more important than clippy
pub fn to_shared(self) -> Shared<T> {
self.into()
}
Expand Down
10 changes: 3 additions & 7 deletions src/font_funcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ pub trait FontFuncs {
y_bearing: font.parent_scale_y_distance(|_| extents.y_bearing),
width: font.parent_scale_x_distance(|_| extents.width),
height: font.parent_scale_y_distance(|_| extents.height),
..extents
})
}
fn get_glyph_contour_point(
Expand All @@ -112,6 +111,7 @@ macro_rules! hb_callback {
$(argument $closure_arg:ty => $expr:expr,)*
return $closure_ret_id:ident: $closure_ret:ty => $ret_expr:expr
}) => {
#[allow(clippy::let_and_return)]
extern "C" fn $func_name<T, F>(
font: *mut hb::hb_font_t,
font_data: *mut c_void,
Expand Down Expand Up @@ -364,9 +364,7 @@ hb_callback!(
/// // implementations of other functions...
/// }
///
/// fn main() {
/// let font_funcs: Owned<FontFuncsImpl<MyFontData>> = FontFuncsImpl::from_trait_impl();
/// }
/// let font_funcs: Owned<FontFuncsImpl<MyFontData>> = FontFuncsImpl::from_trait_impl();
/// ```
pub(crate) struct FontFuncsImpl<T> {
raw: NonNull<hb::hb_font_funcs_t>,
Expand Down Expand Up @@ -404,9 +402,7 @@ impl<T: FontFuncs> FontFuncsImpl<T> {
/// // implement other trait functions...
/// }
///
/// fn main() {
/// let font_funcs: Owned<FontFuncsImpl<MyFontData>> = FontFuncsImpl::from_trait_impl();
/// }
/// let font_funcs: Owned<FontFuncsImpl<MyFontData>> = FontFuncsImpl::from_trait_impl();
/// ```
///
pub fn from_trait_impl() -> Owned<FontFuncsImpl<T>> {
Expand Down
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//! with some text and call the `shape` function.
//!
//! ```
//! # extern crate harfbuzz_rs;
//! use harfbuzz_rs::*;
//!
//! # fn try_main() -> Result<(), std::io::Error> {
Expand Down Expand Up @@ -50,9 +49,7 @@
//! # Ok(())
//! # }
//! #
//! # fn main() {
//! # try_main().unwrap();
//! # }
//! # try_main().unwrap();
//! ```
//! This should print out something similar to the following:
//!
Expand Down
8 changes: 4 additions & 4 deletions src/rusttype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ impl<'a> FontFuncs for ScaledRusttypeFont<'a> {
use std::sync::Arc;

/// Creates a new HarfBuzz `Font` object that uses RustType to provide font data.
///
///
/// # Examples
///
///
/// Create a basic font that uses rusttype font funcs:
/// ```
/// use std::fs;
/// use std::sync::Arc;
///
///
/// use harfbuzz_rs::rusttype::create_harfbuzz_rusttype_font;
///
///
/// let path = "testfiles/SourceSansVariable-Roman.ttf";
/// let bytes: Arc<[u8]> = fs::read(path).unwrap().into();
/// let font = create_harfbuzz_rusttype_font(bytes, 0);
Expand Down

0 comments on commit 35d271e

Please sign in to comment.