mirror of
https://github.com/typst/typst
synced 2025-05-13 20:46:23 +08:00
Needless clone, borrows, casts and lifetimes (#504)
This commit is contained in:
parent
9eb6174b22
commit
5e5b1bba51
@ -279,7 +279,7 @@ fn compile_once(world: &mut SystemWorld, command: &CompileSettings) -> StrResult
|
||||
// Print diagnostics.
|
||||
Err(errors) => {
|
||||
status(command, Status::Error).unwrap();
|
||||
print_diagnostics(&world, *errors)
|
||||
print_diagnostics(world, *errors)
|
||||
.map_err(|_| "failed to print diagnostics")?;
|
||||
Ok(true)
|
||||
}
|
||||
@ -576,10 +576,10 @@ impl PathHash {
|
||||
/// Read a file.
|
||||
fn read(path: &Path) -> FileResult<Vec<u8>> {
|
||||
let f = |e| FileError::from_io(e, path);
|
||||
if fs::metadata(&path).map_err(f)?.is_dir() {
|
||||
if fs::metadata(path).map_err(f)?.is_dir() {
|
||||
Err(FileError::IsDirectory)
|
||||
} else {
|
||||
fs::read(&path).map_err(f)
|
||||
fs::read(path).map_err(f)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -457,7 +457,7 @@ pub fn round(
|
||||
Num::Int(n) if digits == 0 => Value::Int(n),
|
||||
_ => {
|
||||
let n = value.float();
|
||||
let factor = 10.0_f64.powi(digits as i32) as f64;
|
||||
let factor = 10.0_f64.powi(digits as i32);
|
||||
Value::Float((n * factor).round() / factor)
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ impl Layout for FlowElem {
|
||||
let mut styles = styles;
|
||||
if let Some((elem, map)) = child.to_styled() {
|
||||
child = elem;
|
||||
styles = outer.chain(&map);
|
||||
styles = outer.chain(map);
|
||||
}
|
||||
|
||||
if let Some(elem) = child.to::<VElem>() {
|
||||
@ -54,7 +54,7 @@ impl Layout for FlowElem {
|
||||
true,
|
||||
));
|
||||
} else if child.can::<dyn Layout>() {
|
||||
layouter.layout_multiple(vt, &child, styles)?;
|
||||
layouter.layout_multiple(vt, child, styles)?;
|
||||
} else if child.is::<ColbreakElem>() {
|
||||
if !layouter.regions.backlog.is_empty() || layouter.regions.last.is_some()
|
||||
{
|
||||
|
@ -15,6 +15,11 @@ impl Fragment {
|
||||
Self(frames)
|
||||
}
|
||||
|
||||
/// Return `true` if the length is 0.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
|
||||
/// The number of frames in the fragment.
|
||||
pub fn len(&self) -> usize {
|
||||
self.0.len()
|
||||
|
@ -267,7 +267,7 @@ impl<'a, 'v> GridLayouter<'a, 'v> {
|
||||
|
||||
// We use these regions for auto row measurement. Since at that moment,
|
||||
// columns are already sized, we can enable horizontal expansion.
|
||||
let mut regions = regions.clone();
|
||||
let mut regions = regions;
|
||||
regions.expand = Axes::new(true, false);
|
||||
|
||||
Self {
|
||||
|
@ -197,7 +197,7 @@ cast_from_value! {
|
||||
ListMarker,
|
||||
v: Content => Self::Content(vec![v]),
|
||||
array: Array => {
|
||||
if array.len() == 0 {
|
||||
if array.is_empty() {
|
||||
Err("array must contain at least one marker")?;
|
||||
}
|
||||
Self::Content(array.into_iter().map(Value::display).collect())
|
||||
|
@ -172,7 +172,7 @@ fn realize_root<'a>(
|
||||
return Ok((content.clone(), styles));
|
||||
}
|
||||
|
||||
let mut builder = Builder::new(vt, &scratch, true);
|
||||
let mut builder = Builder::new(vt, scratch, true);
|
||||
builder.accept(content, styles)?;
|
||||
builder.interrupt_page(Some(styles))?;
|
||||
let (pages, shared) = builder.doc.unwrap().pages.finish();
|
||||
@ -197,7 +197,7 @@ fn realize_block<'a>(
|
||||
return Ok((content.clone(), styles));
|
||||
}
|
||||
|
||||
let mut builder = Builder::new(vt, &scratch, false);
|
||||
let mut builder = Builder::new(vt, scratch, false);
|
||||
builder.accept(content, styles)?;
|
||||
builder.interrupt_par()?;
|
||||
let (children, shared) = builder.flow.0.finish();
|
||||
@ -314,7 +314,7 @@ impl<'a, 'v, 't> Builder<'a, 'v, 't> {
|
||||
) -> SourceResult<()> {
|
||||
let stored = self.scratch.styles.alloc(styles);
|
||||
let styles = stored.chain(map);
|
||||
self.interrupt_style(&map, None)?;
|
||||
self.interrupt_style(map, None)?;
|
||||
self.accept(elem, styles)?;
|
||||
self.interrupt_style(map, Some(styles))?;
|
||||
Ok(())
|
||||
@ -468,9 +468,9 @@ impl<'a> FlowBuilder<'a> {
|
||||
|
||||
let above = BlockElem::above_in(styles);
|
||||
let below = BlockElem::below_in(styles);
|
||||
self.0.push(above.clone().pack(), styles);
|
||||
self.0.push(above.pack(), styles);
|
||||
self.0.push(content.clone(), styles);
|
||||
self.0.push(below.clone().pack(), styles);
|
||||
self.0.push(below.pack(), styles);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -365,12 +365,13 @@ impl<'a> Item<'a> {
|
||||
}
|
||||
|
||||
/// Maps byte offsets back to spans.
|
||||
#[derive(Default)]
|
||||
pub struct SpanMapper(Vec<(usize, Span)>);
|
||||
|
||||
impl SpanMapper {
|
||||
/// Create a new span mapper.
|
||||
pub fn new() -> Self {
|
||||
Self(vec![])
|
||||
Self::default()
|
||||
}
|
||||
|
||||
/// Push a span for a segment with the given length.
|
||||
@ -745,8 +746,8 @@ fn is_compatible(a: Script, b: Script) -> bool {
|
||||
|
||||
/// Get a style property, but only if it is the same for all children of the
|
||||
/// paragraph.
|
||||
fn shared_get<'a, T: PartialEq>(
|
||||
styles: StyleChain<'a>,
|
||||
fn shared_get<T: PartialEq>(
|
||||
styles: StyleChain<'_>,
|
||||
children: &[Content],
|
||||
getter: fn(StyleChain) -> T,
|
||||
) -> Option<T> {
|
||||
@ -754,7 +755,7 @@ fn shared_get<'a, T: PartialEq>(
|
||||
children
|
||||
.iter()
|
||||
.filter_map(|child| child.to_styled())
|
||||
.all(|(_, local)| getter(styles.chain(&local)) == value)
|
||||
.all(|(_, local)| getter(styles.chain(local)) == value)
|
||||
.then(|| value)
|
||||
}
|
||||
|
||||
|
@ -198,7 +198,7 @@ impl<'a> StackLayouter<'a> {
|
||||
let aligns = if let Some(align) = block.to::<AlignElem>() {
|
||||
align.alignment(styles)
|
||||
} else if let Some((_, local)) = block.to_styled() {
|
||||
AlignElem::alignment_in(styles.chain(&local))
|
||||
AlignElem::alignment_in(styles.chain(local))
|
||||
} else {
|
||||
AlignElem::alignment_in(styles)
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
|
||||
Self {
|
||||
vt,
|
||||
regions: Regions::one(regions.base(), Axes::splat(false)),
|
||||
font: &font,
|
||||
font,
|
||||
ttf: font.ttf(),
|
||||
table,
|
||||
constants,
|
||||
@ -120,7 +120,7 @@ impl<'a, 'b, 'v> MathContext<'a, 'b, 'v> {
|
||||
|
||||
pub fn layout_content(&mut self, content: &Content) -> SourceResult<Frame> {
|
||||
Ok(content
|
||||
.layout(&mut self.vt, self.outer.chain(&self.local), self.regions)?
|
||||
.layout(self.vt, self.outer.chain(&self.local), self.regions)?
|
||||
.into_frame())
|
||||
}
|
||||
|
||||
|
@ -297,7 +297,7 @@ impl LayoutMath for Content {
|
||||
}
|
||||
|
||||
if let Some((elem, styles)) = self.to_styled() {
|
||||
if TextElem::font_in(ctx.styles().chain(&styles))
|
||||
if TextElem::font_in(ctx.styles().chain(styles))
|
||||
!= TextElem::font_in(ctx.styles())
|
||||
{
|
||||
let frame = ctx.layout_content(self)?;
|
||||
|
@ -52,7 +52,7 @@ pub struct BibliographyElem {
|
||||
args.expect::<Spanned<BibPaths>>("path to bibliography file")?;
|
||||
for path in &mut paths.0 {
|
||||
// resolve paths
|
||||
*path = vm.locate(&path).at(span)?.to_string_lossy().into();
|
||||
*path = vm.locate(path).at(span)?.to_string_lossy().into();
|
||||
}
|
||||
// check that parsing works
|
||||
let _ = load(vm.world(), &paths).at(span)?;
|
||||
@ -145,7 +145,7 @@ impl Show for BibliographyElem {
|
||||
|
||||
let mut seq = vec![];
|
||||
if let Some(title) = self.title(styles) {
|
||||
let title = title.clone().unwrap_or_else(|| {
|
||||
let title = title.unwrap_or_else(|| {
|
||||
TextElem::packed(self.local_name(TextElem::lang_in(styles)))
|
||||
.spanned(self.span())
|
||||
});
|
||||
@ -526,7 +526,7 @@ fn create(
|
||||
// Make link from citation to here work.
|
||||
let backlink = {
|
||||
let mut content = Content::empty();
|
||||
content.set_location(ref_location(&reference.entry));
|
||||
content.set_location(ref_location(reference.entry));
|
||||
MetaElem::set_data(vec![Meta::Elem(content)])
|
||||
};
|
||||
|
||||
|
@ -73,7 +73,7 @@ impl Show for OutlineElem {
|
||||
fn show(&self, vt: &mut Vt, styles: StyleChain) -> SourceResult<Content> {
|
||||
let mut seq = vec![ParbreakElem::new().pack()];
|
||||
if let Some(title) = self.title(styles) {
|
||||
let title = title.clone().unwrap_or_else(|| {
|
||||
let title = title.unwrap_or_else(|| {
|
||||
TextElem::packed(self.local_name(TextElem::lang_in(styles)))
|
||||
.spanned(self.span())
|
||||
});
|
||||
|
@ -119,7 +119,7 @@ impl Show for RefElem {
|
||||
.map(TextElem::packed)
|
||||
.unwrap_or_default(),
|
||||
Smart::Custom(None) => Content::empty(),
|
||||
Smart::Custom(Some(Supplement::Content(content))) => content.clone(),
|
||||
Smart::Custom(Some(Supplement::Content(content))) => content,
|
||||
Smart::Custom(Some(Supplement::Func(func))) => {
|
||||
func.call_vt(vt, [elem.clone().into()])?.display()
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ pub fn emoji() -> Module {
|
||||
}
|
||||
|
||||
/// A list of named emoji.
|
||||
const EMOJI: &[(&'static str, Symbol)] = symbols! {
|
||||
const EMOJI: &[(&str, Symbol)] = symbols! {
|
||||
abacus: '🧮',
|
||||
abc: '🔤',
|
||||
abcd: '🔡',
|
||||
|
@ -10,7 +10,7 @@ pub fn sym() -> Module {
|
||||
}
|
||||
|
||||
/// The list of general symbols.
|
||||
pub(crate) const SYM: &[(&'static str, Symbol)] = symbols! {
|
||||
pub(crate) const SYM: &[(&str, Symbol)] = symbols! {
|
||||
// Control.
|
||||
wj: '\u{2060}',
|
||||
zwj: '\u{200D}',
|
||||
|
@ -384,7 +384,7 @@ pub fn shape<'a>(
|
||||
}
|
||||
|
||||
/// Shape text with font fallback using the `families` iterator.
|
||||
fn shape_segment<'a>(
|
||||
fn shape_segment(
|
||||
ctx: &mut ShapingContext,
|
||||
base: usize,
|
||||
text: &str,
|
||||
|
@ -135,7 +135,7 @@ fn search_text(content: &Content, sub: bool) -> Option<EcoString> {
|
||||
} else if let Some(children) = content.to_sequence() {
|
||||
let mut full = EcoString::new();
|
||||
for item in children {
|
||||
match search_text(&item, sub) {
|
||||
match search_text(item, sub) {
|
||||
Some(text) => full.push_str(&text),
|
||||
None => return None,
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ impl Layout for PolygonElem {
|
||||
let mut frame = Frame::new(target);
|
||||
|
||||
// Only create a path if there are more than zero points.
|
||||
if points.len() > 0 {
|
||||
if !points.is_empty() {
|
||||
let fill = self.fill(styles);
|
||||
let stroke = self.stroke(styles).map(PartialStroke::unwrap_or_default);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user