difftreelog
style fix clippy warnings
in: master
8 files changed
crates/jrsonnet-evaluator/src/arr/mod.rsdiffbeforeafterboth1use std::{2 any::Any,3 num::{NonZeroU32, NonZeroUsize},4};56use jrsonnet_gcmodule::{Cc, Trace};7use jrsonnet_interner::IBytes;8use jrsonnet_parser::LocExpr;910use crate::{function::FuncVal, gc::TraceBox, tb, Context, Result, Thunk, Val};1112mod spec;13pub use spec::{ArrayLike, *};1415/// Represents a Jsonnet array value.16#[derive(Debug, Clone, Trace)]17// may contrain other ArrValue18#[trace(tracking(force))]19pub struct ArrValue(Cc<TraceBox<dyn ArrayLike>>);2021pub trait ArrayLikeIter<T>: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator {}22impl<I, T> ArrayLikeIter<T> for I where23 I: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator24{25}2627impl ArrValue {28 pub fn new(v: impl ArrayLike) -> Self {29 Self(Cc::new(tb!(v)))30 }31 pub fn empty() -> Self {32 Self::new(RangeArray::empty())33 }3435 pub fn expr(ctx: Context, exprs: impl IntoIterator<Item = LocExpr>) -> Self {36 Self::new(ExprArray::new(ctx, exprs))37 }3839 pub fn lazy(thunks: Vec<Thunk<Val>>) -> Self {40 Self::new(LazyArray(thunks))41 }4243 pub fn eager(values: Vec<Val>) -> Self {44 Self::new(EagerArray(values))45 }4647 pub fn repeated(data: Self, repeats: usize) -> Option<Self> {48 Some(Self::new(RepeatedArray::new(data, repeats)?))49 }5051 pub fn bytes(bytes: IBytes) -> Self {52 Self::new(BytesArray(bytes))53 }54 pub fn chars(chars: impl Iterator<Item = char>) -> Self {55 Self::new(CharArray(chars.collect()))56 }5758 #[must_use]59 pub fn map(self, mapper: FuncVal) -> Self {60 Self::new(MappedArray::new(self, mapper))61 }6263 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {64 // TODO: ArrValue::Picked(inner, indexes) for large arrays65 let mut out = Vec::new();66 for i in self.iter() {67 let i = i?;68 if filter(&i)? {69 out.push(i);70 };71 }72 Ok(Self::eager(out))73 }7475 pub fn extended(a: Self, b: Self) -> Self {76 // TODO: benchmark for an optimal value, currently just a arbitrary choice77 const ARR_EXTEND_THRESHOLD: usize = 100;7879 if a.is_empty() {80 b81 } else if b.is_empty() {82 a83 } else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {84 Self::new(ExtendedArray::new(a, b))85 } else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {86 let mut out = Vec::with_capacity(a.len() + b.len());87 out.extend(a);88 out.extend(b);89 Self::eager(out)90 } else {91 let mut out = Vec::with_capacity(a.len() + b.len());92 out.extend(a.iter_lazy());93 out.extend(b.iter_lazy());94 Self::lazy(out)95 }96 }9798 pub fn range_exclusive(a: i32, b: i32) -> Self {99 Self::new(RangeArray::new_exclusive(a, b))100 }101 pub fn range_inclusive(a: i32, b: i32) -> Self {102 Self::new(RangeArray::new_inclusive(a, b))103 }104105 /// # Panics106 /// If step == 0107 #[must_use]108 pub fn slice(self, index: Option<i32>, end: Option<i32>, step: Option<NonZeroU32>) -> Self {109 let get_idx = |pos: Option<i32>, len: usize, default| match pos {110 Some(v) if v < 0 => len.saturating_sub((-v) as usize),111 Some(v) => (v as usize).min(len),112 None => default,113 };114 let index = get_idx(index, self.len(), 0);115 let end = get_idx(end, self.len(), self.len());116 let step = step.unwrap_or_else(|| NonZeroU32::new(1).expect("1 != 0"));117118 if index >= end {119 return Self::empty();120 }121122 Self::new(SliceArray {123 inner: self,124 from: index as u32,125 to: end as u32,126 step: step.get(),127 })128 }129130 /// Array length.131 pub fn len(&self) -> usize {132 self.0.len()133 }134135 /// Is array contains no elements?136 pub fn is_empty(&self) -> bool {137 self.0.is_empty()138 }139140 /// Get array element by index, evaluating it, if it is lazy.141 ///142 /// Returns `None` on out-of-bounds condition.143 pub fn get(&self, index: usize) -> Result<Option<Val>> {144 self.0.get(index)145 }146147 /// Returns None if get is either non cheap, or out of bounds148 fn get_cheap(&self, index: usize) -> Option<Val> {149 self.0.get_cheap(index)150 }151152 /// Get array element by index, without evaluation.153 ///154 /// Returns `None` on out-of-bounds condition.155 pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {156 self.0.get_lazy(index)157 }158159 pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {160 (0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))161 }162163 /// Iterate over elements, returning lazy values.164 pub fn iter_lazy(&self) -> impl ArrayLikeIter<Thunk<Val>> + '_ {165 (0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))166 }167168 pub fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {169 if self.is_cheap() {170 Some((0..self.len()).map(|i| self.get_cheap(i).expect("length and is_cheap checked")))171 } else {172 None173 }174 }175176 /// Return a reversed view on current array.177 #[must_use]178 pub fn reversed(self) -> Self {179 Self::new(ReverseArray(self))180 }181182 pub fn ptr_eq(a: &Self, b: &Self) -> bool {183 Cc::ptr_eq(&a.0, &b.0)184 }185186 /// Is this vec supports `.get_cheap()?`187 pub fn is_cheap(&self) -> bool {188 self.0.is_cheap()189 }190191 pub fn as_any(&self) -> &dyn Any {192 &self.0193 }194}195impl From<Vec<Val>> for ArrValue {196 fn from(value: Vec<Val>) -> Self {197 Self::eager(value)198 }199}200impl From<Vec<Thunk<Val>>> for ArrValue {201 fn from(value: Vec<Thunk<Val>>) -> Self {202 Self::lazy(value)203 }204}205impl FromIterator<Val> for ArrValue {206 fn from_iter<T: IntoIterator<Item = Val>>(iter: T) -> Self {207 Self::eager(iter.into_iter().collect())208 }209}210impl ArrayLike for ArrValue {211 fn len(&self) -> usize {212 self.0.len()213 }214215 fn get(&self, index: usize) -> Result<Option<Val>> {216 self.0.get(index)217 }218219 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {220 self.0.get_lazy(index)221 }222223 fn get_cheap(&self, index: usize) -> Option<Val> {224 self.0.get_cheap(index)225 }226227 fn is_cheap(&self) -> bool {228 self.0.is_cheap()229 }230}231232#[cfg(target_pointer_width = "64")]233static_assertions::assert_eq_size!(ArrValue, [u8; 8]);1use std::{any::Any, num::NonZeroU32};23use jrsonnet_gcmodule::{Cc, Trace};4use jrsonnet_interner::IBytes;5use jrsonnet_parser::LocExpr;67use crate::{function::FuncVal, gc::TraceBox, tb, Context, Result, Thunk, Val};89mod spec;10pub use spec::{ArrayLike, *};1112/// Represents a Jsonnet array value.13#[derive(Debug, Clone, Trace)]14// may contrain other ArrValue15#[trace(tracking(force))]16pub struct ArrValue(Cc<TraceBox<dyn ArrayLike>>);1718pub trait ArrayLikeIter<T>: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator {}19impl<I, T> ArrayLikeIter<T> for I where20 I: Iterator<Item = T> + DoubleEndedIterator + ExactSizeIterator21{22}2324impl ArrValue {25 pub fn new(v: impl ArrayLike) -> Self {26 Self(Cc::new(tb!(v)))27 }28 pub fn empty() -> Self {29 Self::new(RangeArray::empty())30 }3132 pub fn expr(ctx: Context, exprs: impl IntoIterator<Item = LocExpr>) -> Self {33 Self::new(ExprArray::new(ctx, exprs))34 }3536 pub fn lazy(thunks: Vec<Thunk<Val>>) -> Self {37 Self::new(LazyArray(thunks))38 }3940 pub fn eager(values: Vec<Val>) -> Self {41 Self::new(EagerArray(values))42 }4344 pub fn repeated(data: Self, repeats: usize) -> Option<Self> {45 Some(Self::new(RepeatedArray::new(data, repeats)?))46 }4748 pub fn bytes(bytes: IBytes) -> Self {49 Self::new(BytesArray(bytes))50 }51 pub fn chars(chars: impl Iterator<Item = char>) -> Self {52 Self::new(CharArray(chars.collect()))53 }5455 #[must_use]56 pub fn map(self, mapper: FuncVal) -> Self {57 Self::new(MappedArray::new(self, mapper))58 }5960 pub fn filter(self, filter: impl Fn(&Val) -> Result<bool>) -> Result<Self> {61 // TODO: ArrValue::Picked(inner, indexes) for large arrays62 let mut out = Vec::new();63 for i in self.iter() {64 let i = i?;65 if filter(&i)? {66 out.push(i);67 };68 }69 Ok(Self::eager(out))70 }7172 pub fn extended(a: Self, b: Self) -> Self {73 // TODO: benchmark for an optimal value, currently just a arbitrary choice74 const ARR_EXTEND_THRESHOLD: usize = 100;7576 if a.is_empty() {77 b78 } else if b.is_empty() {79 a80 } else if a.len() + b.len() > ARR_EXTEND_THRESHOLD {81 Self::new(ExtendedArray::new(a, b))82 } else if let (Some(a), Some(b)) = (a.iter_cheap(), b.iter_cheap()) {83 let mut out = Vec::with_capacity(a.len() + b.len());84 out.extend(a);85 out.extend(b);86 Self::eager(out)87 } else {88 let mut out = Vec::with_capacity(a.len() + b.len());89 out.extend(a.iter_lazy());90 out.extend(b.iter_lazy());91 Self::lazy(out)92 }93 }9495 pub fn range_exclusive(a: i32, b: i32) -> Self {96 Self::new(RangeArray::new_exclusive(a, b))97 }98 pub fn range_inclusive(a: i32, b: i32) -> Self {99 Self::new(RangeArray::new_inclusive(a, b))100 }101102 /// # Panics103 /// If step == 0104 #[must_use]105 pub fn slice(self, index: Option<i32>, end: Option<i32>, step: Option<NonZeroU32>) -> Self {106 let get_idx = |pos: Option<i32>, len: usize, default| match pos {107 Some(v) if v < 0 => len.saturating_sub((-v) as usize),108 Some(v) => (v as usize).min(len),109 None => default,110 };111 let index = get_idx(index, self.len(), 0);112 let end = get_idx(end, self.len(), self.len());113 let step = step.unwrap_or_else(|| NonZeroU32::new(1).expect("1 != 0"));114115 if index >= end {116 return Self::empty();117 }118119 Self::new(SliceArray {120 inner: self,121 from: index as u32,122 to: end as u32,123 step: step.get(),124 })125 }126127 /// Array length.128 pub fn len(&self) -> usize {129 self.0.len()130 }131132 /// Is array contains no elements?133 pub fn is_empty(&self) -> bool {134 self.0.is_empty()135 }136137 /// Get array element by index, evaluating it, if it is lazy.138 ///139 /// Returns `None` on out-of-bounds condition.140 pub fn get(&self, index: usize) -> Result<Option<Val>> {141 self.0.get(index)142 }143144 /// Returns None if get is either non cheap, or out of bounds145 fn get_cheap(&self, index: usize) -> Option<Val> {146 self.0.get_cheap(index)147 }148149 /// Get array element by index, without evaluation.150 ///151 /// Returns `None` on out-of-bounds condition.152 pub fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {153 self.0.get_lazy(index)154 }155156 pub fn iter(&self) -> impl ArrayLikeIter<Result<Val>> + '_ {157 (0..self.len()).map(|i| self.get(i).transpose().expect("length checked"))158 }159160 /// Iterate over elements, returning lazy values.161 pub fn iter_lazy(&self) -> impl ArrayLikeIter<Thunk<Val>> + '_ {162 (0..self.len()).map(|i| self.get_lazy(i).expect("length checked"))163 }164165 pub fn iter_cheap(&self) -> Option<impl ArrayLikeIter<Val> + '_> {166 if self.is_cheap() {167 Some((0..self.len()).map(|i| self.get_cheap(i).expect("length and is_cheap checked")))168 } else {169 None170 }171 }172173 /// Return a reversed view on current array.174 #[must_use]175 pub fn reversed(self) -> Self {176 Self::new(ReverseArray(self))177 }178179 pub fn ptr_eq(a: &Self, b: &Self) -> bool {180 Cc::ptr_eq(&a.0, &b.0)181 }182183 /// Is this vec supports `.get_cheap()?`184 pub fn is_cheap(&self) -> bool {185 self.0.is_cheap()186 }187188 pub fn as_any(&self) -> &dyn Any {189 &self.0190 }191}192impl From<Vec<Val>> for ArrValue {193 fn from(value: Vec<Val>) -> Self {194 Self::eager(value)195 }196}197impl From<Vec<Thunk<Val>>> for ArrValue {198 fn from(value: Vec<Thunk<Val>>) -> Self {199 Self::lazy(value)200 }201}202impl FromIterator<Val> for ArrValue {203 fn from_iter<T: IntoIterator<Item = Val>>(iter: T) -> Self {204 Self::eager(iter.into_iter().collect())205 }206}207impl ArrayLike for ArrValue {208 fn len(&self) -> usize {209 self.0.len()210 }211212 fn get(&self, index: usize) -> Result<Option<Val>> {213 self.0.get(index)214 }215216 fn get_lazy(&self, index: usize) -> Option<Thunk<Val>> {217 self.0.get_lazy(index)218 }219220 fn get_cheap(&self, index: usize) -> Option<Val> {221 self.0.get_cheap(index)222 }223224 fn is_cheap(&self) -> bool {225 self.0.is_cheap()226 }227}228229#[cfg(target_pointer_width = "64")]230static_assertions::assert_eq_size!(ArrValue, [u8; 8]);crates/jrsonnet-evaluator/src/evaluate/destructure.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/destructure.rs
@@ -110,7 +110,11 @@
fn get(self: Box<Self>) -> Result<Self::Output> {
let full = self.full.evaluate()?;
let to = full.len() - self.end;
- Ok(Val::Arr(full.slice(Some(self.start as i32), Some(to as i32), None)))
+ Ok(Val::Arr(full.slice(
+ Some(self.start as i32),
+ Some(to as i32),
+ None,
+ )))
}
}
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -2,7 +2,7 @@
cell::RefCell,
fmt::{self, Debug, Display},
mem::replace,
- num::{NonZeroU32, NonZeroUsize},
+ num::NonZeroU32,
rc::Rc,
};
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -218,8 +218,7 @@
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let attr = parse_macro_input!(attr as BuiltinAttrs);
- let item_fn = item.clone();
- let item_fn: ItemFn = parse_macro_input!(item_fn);
+ let item_fn = parse_macro_input!(item as ItemFn);
match builtin_inner(attr, item_fn) {
Ok(v) => v.into(),
crates/jrsonnet-stdlib/src/manifest/toml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/toml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/toml.rs
@@ -4,7 +4,7 @@
bail,
manifest::{escape_string_json_buf, ManifestFormat},
val::ArrValue,
- IStr, ObjValue, Result, ResultExt, Val, State,
+ IStr, ObjValue, Result, ResultExt, State, Val,
};
pub struct TomlFormat<'s> {
crates/jrsonnet-stdlib/src/manifest/xml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/xml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/xml.rs
@@ -1,9 +1,9 @@
use jrsonnet_evaluator::{
bail,
manifest::{ManifestFormat, ToStringFormat},
- typed::{ComplexValType, Either2, Either4, Typed, ValType},
- val::{ArrValue, IndexableVal},
- Either, ObjValue, Result, ResultExt, Val, State,
+ typed::{ComplexValType, Either2, Typed, ValType},
+ val::ArrValue,
+ Either, ObjValue, Result, ResultExt, State, Val,
};
pub struct XmlJsonmlFormat {
@@ -39,20 +39,20 @@
fn from_untyped(untyped: Val) -> Result<Self> {
let val = <Either![ArrValue, String]>::from_untyped(untyped)
- .with_description(|| format!("parsing JSONML value (an array or string)"))?;
+ .description("parsing JSONML value (an array or string)")?;
let arr = match val {
Either2::A(a) => a,
Either2::B(s) => return Ok(Self::String(s)),
};
- if arr.len() < 1 {
+ if arr.is_empty() {
bail!("JSONML value should have tag (array length should be >=1)");
};
let tag = String::from_untyped(
arr.get(0)
- .with_description(|| "getting JSONML tag")?
+ .description("getting JSONML tag")?
.expect("length checked"),
)
- .with_description(|| format!("parsing JSONML tag"))?;
+ .description("parsing JSONML tag")?;
let (has_attrs, attrs) = if arr.len() >= 2 {
let maybe_attrs = arr
@@ -71,7 +71,7 @@
tag,
attrs,
children: State::push_description(
- || format!("parsing children"),
+ || "parsing children".to_owned(),
|| {
Typed::from_untyped(Val::Arr(arr.slice(
Some(if has_attrs { 2 } else { 1 }),
@@ -100,7 +100,7 @@
} => {
let has_children = !children.is_empty();
buf.push('<');
- buf.push_str(&tag);
+ buf.push_str(tag);
attrs.run_assertions()?;
for (key, value) in attrs.iter(
// Not much sense to preserve order here
@@ -125,12 +125,12 @@
}
buf.push('>');
for child in children {
- manifest_jsonml(&child, buf, opts)?;
+ manifest_jsonml(child, buf, opts)?;
}
if has_children || opts.force_closing {
buf.push('<');
buf.push('/');
- buf.push_str(&tag);
+ buf.push_str(tag);
buf.push('>');
}
Ok(())
@@ -177,8 +177,8 @@
}
if !found {
// No match - no escapes required
- out.push_str(&str);
+ out.push_str(str);
return;
}
- out.push_str(&remaining);
+ out.push_str(remaining);
}
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/misc.rs
+++ b/crates/jrsonnet-stdlib/src/misc.rs
@@ -28,8 +28,7 @@
o: ObjValue,
f: IStr,
default: Option<Thunk<Val>>,
- #[default(true)]
- inc_hidden: bool,
+ #[default(true)] inc_hidden: bool,
) -> Result<Val> {
let do_default = move || {
let Some(default) = default else {
crates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -57,8 +57,7 @@
o: ObjValue,
include_hidden: bool,
- #[cfg(feature = "exp-preserve-order")]
- preserve_order: bool,
+ #[cfg(feature = "exp-preserve-order")] preserve_order: bool,
) -> ArrValue {
o.values_ex(
include_hidden,
@@ -101,8 +100,7 @@
o: ObjValue,
include_hidden: bool,
- #[cfg(feature = "exp-preserve-order")]
- preserve_order: bool,
+ #[cfg(feature = "exp-preserve-order")] preserve_order: bool,
) -> ArrValue {
o.key_values_ex(
include_hidden,