Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exchange Arc<Vec<u8>> for Arc<Cow<'a, [u8]>> #46

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "dwrote"
description = "Lightweight binding to DirectWrite."
repository = "https://github.com/servo/dwrote-rs"
license = "MPL-2.0"
version = "0.11.0"
version = "0.12.0"
authors = ["The Servo Project Developers", "Vladimir Vukicevic <vladimir@pobox.com>"]
edition = "2018"

Expand Down
5 changes: 3 additions & 2 deletions src/font_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::borrow::Cow;
use std::cell::UnsafeCell;
use std::ffi::OsString;
use std::os::windows::ffi::{OsStrExt, OsStringExt};
Expand Down Expand Up @@ -63,7 +64,7 @@ impl FontFile {
}
}

pub fn new_from_data(data: Arc<Vec<u8>>) -> Option<FontFile> {
pub fn new_from_data(data: Arc<Cow<'static, [u8]>>) -> Option<FontFile> {
let (font_file, font_file_stream, key) = DataFontHelper::register_font_data(data);

let mut ff = FontFile {
Expand All @@ -80,7 +81,7 @@ impl FontFile {
}
}

pub fn analyze_data(data: Arc<Vec<u8>>) -> u32 {
pub fn analyze_data(data: Arc<Cow<'static, [u8]>>) -> u32 {
let (font_file, font_file_stream, key) = DataFontHelper::register_font_data(data);

let mut ff = FontFile {
Expand Down
19 changes: 10 additions & 9 deletions src/font_file_loader_impl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![allow(non_snake_case, non_upper_case_globals)]

use std::borrow::Cow;
use std::collections::HashMap;
use std::marker::Send;
use std::sync::atomic::AtomicUsize;
Expand Down Expand Up @@ -77,10 +78,10 @@ impl FontFileLoader {
unsafe impl Send for FontFileLoader {}
unsafe impl Sync for FontFileLoader {}

struct FontFileStream {
struct FontFileStream<'a> {
refcount: atomic::AtomicUsize,
key: usize,
data: Arc<Vec<u8>>,
data: Arc<Cow<'a, [u8]>>,
}

const FontFileStreamVtbl: &'static IDWriteFontFileStreamVtbl = &IDWriteFontFileStreamVtbl {
Expand Down Expand Up @@ -134,8 +135,8 @@ const FontFileStreamVtbl: &'static IDWriteFontFileStreamVtbl = &IDWriteFontFileS
},
};

impl FontFileStream {
pub fn new(key: usize, data: Arc<Vec<u8>>) -> FontFileStream {
impl<'a> FontFileStream<'a> {
pub fn new(key: usize, data: Arc<Cow<'a, [u8]>>) -> FontFileStream {
FontFileStream {
refcount: AtomicUsize::new(1),
key,
Expand All @@ -144,20 +145,20 @@ impl FontFileStream {
}
}

impl Drop for FontFileStream {
impl<'a> Drop for FontFileStream<'a> {
fn drop(&mut self) {
DataFontHelper::unregister_font_data(self.key);
}
}

impl Com<IDWriteFontFileStream> for FontFileStream {
impl<'a> Com<IDWriteFontFileStream> for FontFileStream<'a> {
type Vtbl = IDWriteFontFileStreamVtbl;
fn vtbl() -> &'static IDWriteFontFileStreamVtbl {
FontFileStreamVtbl
}
}

impl Com<IUnknown> for FontFileStream {
impl<'a> Com<IUnknown> for FontFileStream<'a> {
type Vtbl = IUnknownVtbl;
fn vtbl() -> &'static IUnknownVtbl {
&FontFileStreamVtbl.parent
Expand Down Expand Up @@ -193,8 +194,8 @@ lazy_static! {
pub struct DataFontHelper;

impl DataFontHelper {
pub fn register_font_data(
font_data: Arc<Vec<u8>>,
pub fn register_font_data<'a>(
font_data: Arc<Cow<'a, [u8]>>,
) -> (
ComPtr<IDWriteFontFile>,
ComPtr<IDWriteFontFileStream>,
Expand Down