difftreelog
feat impl Borrow for IStr/IBytes
in: master
2 files changed
crates/jrsonnet-interner/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-interner/src/lib.rs
+++ b/crates/jrsonnet-interner/src/lib.rs
@@ -6,7 +6,7 @@
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(clippy::missing_const_for_fn)]
use std::{
- borrow::Cow,
+ borrow::{Borrow, Cow},
cell::RefCell,
fmt::{self, Display},
hash::{BuildHasherDefault, Hash, Hasher},
@@ -57,6 +57,17 @@
}
}
+impl Borrow<str> for IStr {
+ fn borrow(&self) -> &str {
+ self.as_str()
+ }
+}
+impl Borrow<[u8]> for IStr {
+ fn borrow(&self) -> &[u8] {
+ self.as_bytes()
+ }
+}
+
impl PartialEq for IStr {
fn eq(&self, other: &Self) -> bool {
// all IStr should be inlined into same pool
@@ -135,6 +146,12 @@
}
}
+impl Borrow<[u8]> for IBytes {
+ fn borrow(&self) -> &[u8] {
+ self.0.as_slice()
+ }
+}
+
impl PartialEq for IBytes {
fn eq(&self, other: &Self) -> bool {
// all IStr should be inlined into same pool
crates/jrsonnet-stdlib/src/misc.rsdiffbeforeafterboth1use std::{cell::RefCell, rc::Rc};23use jrsonnet_evaluator::{4 error::{ErrorKind::*, Result},5 function::{builtin, ArgLike, CallLocation, FuncVal},6 throw,7 typed::{Either2, Either4},8 val::{equals, ArrValue},9 Context, Either, IStr, ObjValue, Thunk, Val,10};1112use crate::{extvar_source, Settings};1314#[builtin]15pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {16 use Either4::*;17 match x {18 A(x) => x.chars().count(),19 B(x) => x.len(),20 C(x) => x.len(),21 D(f) => f.params_len(),22 }23}2425#[builtin(fields(26 settings: Rc<RefCell<Settings>>,27))]28pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {29 let ctx = ctx.state().create_default_context(extvar_source(&x, ""));30 this.settings31 .borrow()32 .ext_vars33 .get(&x)34 .cloned()35 .ok_or_else(|| UndefinedExternalVariable(x))?36 .evaluate_arg(ctx, true)?37 .evaluate()38}3940#[builtin(fields(41 settings: Rc<RefCell<Settings>>,42))]43pub fn builtin_native(this: &builtin_native, x: IStr) -> Val {44 this.settings45 .borrow()46 .ext_natives47 .get(&x)48 .cloned()49 .map_or(Val::Null, |v| Val::Func(FuncVal::Builtin(v)))50}5152#[builtin(fields(53 settings: Rc<RefCell<Settings>>,54))]55pub fn builtin_trace(56 this: &builtin_trace,57 loc: CallLocation,58 str: Val,59 rest: Thunk<Val>,60) -> Result<Val> {61 this.settings.borrow().trace_printer.print_trace(62 loc,63 match str {64 Val::Str(s) => s.into_flat(),65 Val::Func(f) => format!("{f:?}").into(),66 v => v67 .manifest(JsonFormat::std_to_json(68 String::from(" "),69 "\n",70 ": ",71 #[cfg(feature = "exp-preserve-order")]72 true,73 ))?74 .into(),75 },76 );77 rest.evaluate()78}7980#[allow(clippy::comparison_chain)]81#[builtin]82pub fn builtin_starts_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {83 Ok(match (a, b) {84 (Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),85 (Either2::B(a), Either2::B(b)) => {86 if b.len() > a.len() {87 return Ok(false);88 } else if b.len() == a.len() {89 return equals(&Val::Arr(a), &Val::Arr(b));90 } else {91 for (a, b) in a.iter().take(b.len()).zip(b.iter()) {92 let a = a?;93 let b = b?;94 if !equals(&a, &b)? {95 return Ok(false);96 }97 }98 true99 }100 }101 _ => throw!("both arguments should be of the same type"),102 })103}104105#[allow(clippy::comparison_chain)]106#[builtin]107pub fn builtin_ends_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {108 Ok(match (a, b) {109 (Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),110 (Either2::B(a), Either2::B(b)) => {111 if b.len() > a.len() {112 return Ok(false);113 } else if b.len() == a.len() {114 return equals(&Val::Arr(a), &Val::Arr(b));115 } else {116 let a_len = a.len();117 for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {118 let a = a?;119 let b = b?;120 if !equals(&a, &b)? {121 return Ok(false);122 }123 }124 true125 }126 }127 _ => throw!("both arguments should be of the same type"),128 })129}1use std::{cell::RefCell, rc::Rc};23use jrsonnet_evaluator::{4 error::{ErrorKind::*, Result},5 function::{builtin, ArgLike, CallLocation, FuncVal},6 manifest::JsonFormat,7 throw,8 typed::{Either2, Either4},9 val::{equals, ArrValue},10 Context, Either, IStr, ObjValue, Thunk, Val,11};1213use crate::{extvar_source, Settings};1415#[builtin]16pub fn builtin_length(x: Either![IStr, ArrValue, ObjValue, FuncVal]) -> usize {17 use Either4::*;18 match x {19 A(x) => x.chars().count(),20 B(x) => x.len(),21 C(x) => x.len(),22 D(f) => f.params_len(),23 }24}2526#[builtin(fields(27 settings: Rc<RefCell<Settings>>,28))]29pub fn builtin_ext_var(this: &builtin_ext_var, ctx: Context, x: IStr) -> Result<Val> {30 let ctx = ctx.state().create_default_context(extvar_source(&x, ""));31 this.settings32 .borrow()33 .ext_vars34 .get(&x)35 .cloned()36 .ok_or_else(|| UndefinedExternalVariable(x))?37 .evaluate_arg(ctx, true)?38 .evaluate()39}4041#[builtin(fields(42 settings: Rc<RefCell<Settings>>,43))]44pub fn builtin_native(this: &builtin_native, x: IStr) -> Val {45 this.settings46 .borrow()47 .ext_natives48 .get(&x)49 .cloned()50 .map_or(Val::Null, |v| Val::Func(FuncVal::Builtin(v)))51}5253#[builtin(fields(54 settings: Rc<RefCell<Settings>>,55))]56pub fn builtin_trace(57 this: &builtin_trace,58 loc: CallLocation,59 str: Val,60 rest: Thunk<Val>,61) -> Result<Val> {62 this.settings.borrow().trace_printer.print_trace(63 loc,64 match str {65 Val::Str(s) => s.into_flat(),66 Val::Func(f) => format!("{f:?}").into(),67 v => v68 .manifest(JsonFormat::std_to_json(69 String::from(" "),70 "\n",71 ": ",72 #[cfg(feature = "exp-preserve-order")]73 true,74 ))?75 .into(),76 },77 );78 rest.evaluate()79}8081#[allow(clippy::comparison_chain)]82#[builtin]83pub fn builtin_starts_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {84 Ok(match (a, b) {85 (Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),86 (Either2::B(a), Either2::B(b)) => {87 if b.len() > a.len() {88 return Ok(false);89 } else if b.len() == a.len() {90 return equals(&Val::Arr(a), &Val::Arr(b));91 } else {92 for (a, b) in a.iter().take(b.len()).zip(b.iter()) {93 let a = a?;94 let b = b?;95 if !equals(&a, &b)? {96 return Ok(false);97 }98 }99 true100 }101 }102 _ => throw!("both arguments should be of the same type"),103 })104}105106#[allow(clippy::comparison_chain)]107#[builtin]108pub fn builtin_ends_with(a: Either![IStr, ArrValue], b: Either![IStr, ArrValue]) -> Result<bool> {109 Ok(match (a, b) {110 (Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),111 (Either2::B(a), Either2::B(b)) => {112 if b.len() > a.len() {113 return Ok(false);114 } else if b.len() == a.len() {115 return equals(&Val::Arr(a), &Val::Arr(b));116 } else {117 let a_len = a.len();118 for (a, b) in a.iter().skip(a_len - b.len()).zip(b.iter()) {119 let a = a?;120 let b = b?;121 if !equals(&a, &b)? {122 return Ok(false);123 }124 }125 true126 }127 }128 _ => throw!("both arguments should be of the same type"),129 })130}