Skip to content

Commit

Permalink
make SmallVec optional
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB committed Sep 2, 2024
1 parent 1937659 commit ac5879d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ rangemap = "1.4.0"
rustc-hash = { version = "1.1.0", default-features = false }
rustybuzz = { version = "0.14", default-features = false, features = ["libm"] }
self_cell = "1.0.1"
smallvec = { version = "1.13.2" }
smallvec = { version = "1.13.2", optional = true }
smol_str = { version = "0.2.2", default-features = false }
swash = { version = "0.1.17", optional = true }
syntect = { version = "5.1.0", optional = true }
Expand All @@ -43,6 +43,7 @@ fontconfig = ["fontdb/fontconfig", "std"]
monospace_fallback = []
no_std = ["rustybuzz/libm", "hashbrown", "dep:libm"]
shape-run-cache = []
small-buffer-opt = ["smallvec"]
std = [
"fontdb/memmap",
"fontdb/std",
Expand Down
11 changes: 5 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use core::{cmp, fmt};
use smallvec::SmallVec;
use unicode_segmentation::UnicodeSegmentation;

use crate::{
Affinity, Align, Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine, Color,
Cursor, FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, LineEnding, LineIter, Motion,
Scroll, ShapeBuffer, ShapeLine, Shaping, Wrap,
Affinity, Align, Attrs, AttrsList, BidiParagraphs, BorrowedWithFontSystem, BufferLine,
BufferVec, Color, Cursor, FontSystem, LayoutCursor, LayoutGlyph, LayoutLine, LineEnding,
LineIter, Motion, Scroll, ShapeBuffer, ShapeLine, Shaping, Wrap,
};

/// A line of visible text for rendering
Expand Down Expand Up @@ -204,7 +203,7 @@ impl fmt::Display for Metrics {
#[derive(Debug)]
pub struct Buffer {
/// [BufferLine]s (or paragraphs) of text in the buffer
pub lines: SmallVec<[BufferLine; 1]>,
pub lines: BufferVec<BufferLine>,
metrics: Metrics,
width_opt: Option<f32>,
height_opt: Option<f32>,
Expand Down Expand Up @@ -251,7 +250,7 @@ impl Buffer {
pub fn new_empty(metrics: Metrics) -> Self {
assert_ne!(metrics.line_height, 0.0, "line height cannot be 0");
Self {
lines: SmallVec::new(),
lines: BufferVec::new(),
metrics,
width_opt: None,
height_opt: None,
Expand Down
11 changes: 5 additions & 6 deletions src/buffer_line.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#[cfg(not(feature = "std"))]
use alloc::{string::String, vec::Vec};
use core::mem;
use smallvec::SmallVec;

use crate::{
Align, Attrs, AttrsList, Cached, FontSystem, LayoutLine, LineEnding, ShapeBuffer, ShapeLine,
Shaping, Wrap,
Align, Attrs, AttrsList, BufferVec, Cached, FontSystem, LayoutLine, LineEnding, ShapeBuffer,
ShapeLine, Shaping, Wrap,
};

/// A line (or paragraph) of text that is shaped and laid out
Expand All @@ -16,7 +15,7 @@ pub struct BufferLine {
attrs_list: AttrsList,
align: Option<Align>,
shape_opt: Cached<ShapeLine>,
layout_opt: Cached<SmallVec<[LayoutLine; 1]>>,
layout_opt: Cached<BufferVec<LayoutLine>>,
shaping: Shaping,
metadata: Option<usize>,
}
Expand Down Expand Up @@ -277,7 +276,7 @@ impl BufferLine {
let mut layout = self
.layout_opt
.take_unused()
.unwrap_or_else(|| SmallVec::default());
.unwrap_or_else(|| BufferVec::default());
let shape = self.shape_in_buffer(scratch, font_system, tab_width);
shape.layout_to_buffer(
scratch,
Expand All @@ -294,7 +293,7 @@ impl BufferLine {
}

/// Get line layout cache
pub fn layout_opt(&self) -> Option<&SmallVec<[LayoutLine; 1]>> {
pub fn layout_opt(&self) -> Option<&BufferVec<LayoutLine>> {
self.layout_opt.get()
}

Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,10 @@ type BuildHasher = core::hash::BuildHasherDefault<rustc_hash::FxHasher>;
type HashMap<K, V> = std::collections::HashMap<K, V, BuildHasher>;
#[cfg(not(feature = "std"))]
type HashMap<K, V> = hashbrown::HashMap<K, V, BuildHasher>;

#[cfg(all(not(feature = "small-buffer-opt"), feature = "std"))]
type BufferVec<T> = Vec<T>;
#[cfg(all(not(feature = "small-buffer-opt"), not(feature = "std")))]
type BufferVec<T> = alloc::vec::Vec<T>;
#[cfg(feature = "small-buffer-opt")]
type BufferVec<T> = smallvec::SmallVec<[T; 1]>;
19 changes: 9 additions & 10 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ use core::cmp::{max, min};
use core::fmt;
use core::mem;
use core::ops::Range;
use smallvec::SmallVec;
use unicode_script::{Script, UnicodeScript};
use unicode_segmentation::UnicodeSegmentation;

use crate::fallback::FontFallbackIter;
use crate::{
math, Align, AttrsList, CacheKeyFlags, Color, Font, FontSystem, LayoutGlyph, LayoutLine,
Metrics, ShapePlanCache, Wrap,
math, Align, AttrsList, BufferVec, CacheKeyFlags, Color, Font, FontSystem, LayoutGlyph,
LayoutLine, Metrics, ShapePlanCache, Wrap,
};

/// The shaping strategy of some text.
Expand Down Expand Up @@ -690,7 +689,7 @@ impl ShapeWord {
#[derive(Clone, Debug)]
pub struct ShapeSpan {
pub level: unicode_bidi::Level,
pub words: SmallVec<[ShapeWord; 1]>,
pub words: BufferVec<ShapeWord>,
}

impl ShapeSpan {
Expand All @@ -700,7 +699,7 @@ impl ShapeSpan {
pub(crate) fn empty() -> Self {
Self {
level: unicode_bidi::Level::ltr(),
words: SmallVec::default(),
words: BufferVec::default(),
}
}

Expand Down Expand Up @@ -857,7 +856,7 @@ impl ShapeSpan {
#[derive(Clone, Debug)]
pub struct ShapeLine {
pub rtl: bool,
pub spans: SmallVec<[ShapeSpan; 1]>,
pub spans: BufferVec<ShapeSpan>,
pub metrics_opt: Option<Metrics>,
}

Expand Down Expand Up @@ -886,7 +885,7 @@ impl ShapeLine {
pub(crate) fn empty() -> Self {
Self {
rtl: false,
spans: SmallVec::default(),
spans: BufferVec::default(),
metrics_opt: None,
}
}
Expand Down Expand Up @@ -1157,8 +1156,8 @@ impl ShapeLine {
wrap: Wrap,
align: Option<Align>,
match_mono_width: Option<f32>,
) -> SmallVec<[LayoutLine; 1]> {
let mut lines = SmallVec::default();
) -> BufferVec<LayoutLine> {
let mut lines = BufferVec::default();
self.layout_to_buffer(
&mut ShapeBuffer::default(),
font_size,
Expand All @@ -1178,7 +1177,7 @@ impl ShapeLine {
width_opt: Option<f32>,
wrap: Wrap,
align: Option<Align>,
layout_lines: &mut SmallVec<[LayoutLine; 1]>,
layout_lines: &mut BufferVec<LayoutLine>,
match_mono_width: Option<f32>,
) {
// For each visual line a list of (span index, and range of words in that span)
Expand Down

0 comments on commit ac5879d

Please sign in to comment.