difftreelog
feat byte array specialization
in: master
2 files changed
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,5 +1,5 @@
use crate::function::StaticBuiltin;
-use crate::typed::{Any, PositiveF64, VecVal, M1};
+use crate::typed::{Any, Bytes, PositiveF64, VecVal, M1};
use crate::{
builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
equals,
@@ -449,17 +449,15 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_encode_utf8(str: IStr) -> Result<VecVal> {
- Ok(VecVal(
- str.bytes()
- .map(|b| Val::Num(b as f64))
- .collect::<Vec<Val>>(),
- ))
+fn builtin_encode_utf8(str: IStr) -> Result<Bytes> {
+ Ok(Bytes(str.bytes().map(|b| b).collect::<Vec<u8>>().into()))
}
#[jrsonnet_macros::builtin]
-fn builtin_decode_utf8(arr: Vec<u8>) -> Result<String> {
- Ok(String::from_utf8(arr).map_err(|_| RuntimeError("bad utf8".into()))?)
+fn builtin_decode_utf8(arr: Bytes) -> Result<IStr> {
+ Ok(std::str::from_utf8(&arr.0)
+ .map_err(|_| RuntimeError("bad utf8".into()))?
+ .into())
}
#[jrsonnet_macros::builtin]
@@ -485,17 +483,21 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_base64(input: Either![Vec<u8>, IStr]) -> Result<String> {
+fn builtin_base64(input: Either![Bytes, IStr]) -> Result<String> {
use Either2::*;
Ok(match input {
- A(a) => base64::encode(a),
+ A(a) => base64::encode(a.0),
B(l) => base64::encode(l.bytes().collect::<Vec<_>>()),
})
}
#[jrsonnet_macros::builtin]
-fn builtin_base64_decode_bytes(input: IStr) -> Result<Vec<u8>> {
- Ok(base64::decode(&input.as_bytes()).map_err(|_| RuntimeError("bad base64".into()))?)
+fn builtin_base64_decode_bytes(input: IStr) -> Result<Bytes> {
+ Ok(Bytes(
+ base64::decode(&input.as_bytes())
+ .map_err(|_| RuntimeError("bad base64".into()))?
+ .into(),
+ ))
}
#[jrsonnet_macros::builtin]
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth1use std::{2 convert::{TryFrom, TryInto},3 rc::Rc,4};56use gcmodule::Cc;7use jrsonnet_interner::IStr;8use jrsonnet_types::{ComplexValType, ValType};910use crate::{11 error::{Error::*, LocError, Result},12 throw,13 typed::CheckType,14 ArrValue, FuncVal, IndexableVal, ObjValue, Val,15};1617pub trait Typed: TryFrom<Val, Error = LocError> + TryInto<Val, Error = LocError> {18 const TYPE: &'static ComplexValType;19}2021macro_rules! impl_int {22 ($($ty:ty)*) => {$(23 impl Typed for $ty {24 const TYPE: &'static ComplexValType =25 &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));26 }27 impl TryFrom<Val> for $ty {28 type Error = LocError;2930 fn try_from(value: Val) -> Result<Self> {31 <Self as Typed>::TYPE.check(&value)?;32 match value {33 Val::Num(n) => {34 if n.trunc() != n {35 throw!(RuntimeError(36 format!(37 "cannot convert number with fractional part to {}",38 stringify!($ty)39 )40 .into()41 ))42 }43 Ok(n as Self)44 }45 _ => unreachable!(),46 }47 }48 }49 impl TryFrom<$ty> for Val {50 type Error = LocError;5152 fn try_from(value: $ty) -> Result<Self> {53 Ok(Self::Num(value as f64))54 }55 }56 )*};57}5859impl_int!(i8 u8 i16 u16 i32 u32);6061impl Typed for f64 {62 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Num);63}64impl TryFrom<Val> for f64 {65 type Error = LocError;6667 fn try_from(value: Val) -> Result<Self> {68 <Self as Typed>::TYPE.check(&value)?;69 match value {70 Val::Num(n) => Ok(n),71 _ => unreachable!(),72 }73 }74}75impl TryFrom<f64> for Val {76 type Error = LocError;7778 fn try_from(value: f64) -> Result<Self> {79 Ok(Self::Num(value))80 }81}8283pub struct PositiveF64(pub f64);84impl Typed for PositiveF64 {85 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(0.0), None);86}87impl TryFrom<Val> for PositiveF64 {88 type Error = LocError;8990 fn try_from(value: Val) -> Result<Self> {91 <Self as Typed>::TYPE.check(&value)?;92 match value {93 Val::Num(n) => Ok(Self(n)),94 _ => unreachable!(),95 }96 }97}98impl TryFrom<PositiveF64> for Val {99 type Error = LocError;100101 fn try_from(value: PositiveF64) -> Result<Self> {102 Ok(Self::Num(value.0))103 }104}105106impl Typed for usize {107 // It is possible to store 54 bits of precision in f64, but leaving u32::MAX here for compatibility108 const TYPE: &'static ComplexValType =109 &ComplexValType::BoundedNumber(Some(0.0), Some(4294967295.0));110}111impl TryFrom<Val> for usize {112 type Error = LocError;113114 fn try_from(value: Val) -> Result<Self> {115 <Self as Typed>::TYPE.check(&value)?;116 match value {117 Val::Num(n) => {118 if n.trunc() != n {119 throw!(RuntimeError(120 "cannot convert number with fractional part to usize".into()121 ))122 }123 Ok(n as Self)124 }125 _ => unreachable!(),126 }127 }128}129impl TryFrom<usize> for Val {130 type Error = LocError;131132 fn try_from(value: usize) -> Result<Self> {133 if value > u32::MAX as usize {134 throw!(RuntimeError("number is too large".into()))135 }136 Ok(Self::Num(value as f64))137 }138}139140impl Typed for IStr {141 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);142}143impl TryFrom<Val> for IStr {144 type Error = LocError;145146 fn try_from(value: Val) -> Result<Self> {147 <Self as Typed>::TYPE.check(&value)?;148 match value {149 Val::Str(s) => Ok(s),150 _ => unreachable!(),151 }152 }153}154impl TryFrom<IStr> for Val {155 type Error = LocError;156157 fn try_from(value: IStr) -> Result<Self> {158 Ok(Self::Str(value))159 }160}161162impl Typed for String {163 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);164}165impl TryFrom<Val> for String {166 type Error = LocError;167168 fn try_from(value: Val) -> Result<Self> {169 <Self as Typed>::TYPE.check(&value)?;170 match value {171 Val::Str(s) => Ok(s.to_string()),172 _ => unreachable!(),173 }174 }175}176impl TryFrom<String> for Val {177 type Error = LocError;178179 fn try_from(value: String) -> Result<Self> {180 Ok(Self::Str(value.into()))181 }182}183184impl Typed for char {185 const TYPE: &'static ComplexValType = &ComplexValType::Char;186}187impl TryFrom<Val> for char {188 type Error = LocError;189190 fn try_from(value: Val) -> Result<Self> {191 <Self as Typed>::TYPE.check(&value)?;192 match value {193 Val::Str(s) => Ok(s.chars().next().unwrap()),194 _ => unreachable!(),195 }196 }197}198impl TryFrom<char> for Val {199 type Error = LocError;200201 fn try_from(value: char) -> Result<Self> {202 Ok(Self::Str(value.to_string().into()))203 }204}205206impl<T> Typed for Vec<T>207where208 T: Typed,209 T: TryFrom<Val, Error = LocError>,210 T: TryInto<Val, Error = LocError>,211{212 const TYPE: &'static ComplexValType = &ComplexValType::ArrayRef(T::TYPE);213}214impl<T> TryFrom<Val> for Vec<T>215where216 T: Typed,217 T: TryFrom<Val, Error = LocError>,218 T: TryInto<Val, Error = LocError>,219{220 type Error = LocError;221222 fn try_from(value: Val) -> Result<Self> {223 <Self as Typed>::TYPE.check(&value)?;224 match value {225 Val::Arr(a) => {226 let mut o = Self::with_capacity(a.len());227 for i in a.iter() {228 o.push(T::try_from(i?)?);229 }230 Ok(o)231 }232 _ => unreachable!(),233 }234 }235}236impl<T> TryFrom<Vec<T>> for Val237where238 T: Typed,239 T: TryFrom<Self, Error = LocError>,240 T: TryInto<Self, Error = LocError>,241{242 type Error = LocError;243244 fn try_from(value: Vec<T>) -> Result<Self> {245 let mut o = Vec::with_capacity(value.len());246 for i in value {247 o.push(i.try_into()?);248 }249 Ok(Self::Arr(o.into()))250 }251}252253/// To be used in Vec<Any>254/// Regular Val can't be used here, because it has wrong TryFrom::Error type255#[derive(Clone)]256pub struct Any(pub Val);257258impl Typed for Any {259 const TYPE: &'static ComplexValType = &ComplexValType::Any;260}261impl TryFrom<Val> for Any {262 type Error = LocError;263264 fn try_from(value: Val) -> Result<Self> {265 Ok(Self(value))266 }267}268impl TryFrom<Any> for Val {269 type Error = LocError;270271 fn try_from(value: Any) -> Result<Self> {272 Ok(value.0)273 }274}275276/// Specialization, provides faster TryFrom<VecVal> for Val277pub struct VecVal(pub Vec<Val>);278279impl Typed for VecVal {280 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);281}282impl TryFrom<Val> for VecVal {283 type Error = LocError;284285 fn try_from(value: Val) -> Result<Self> {286 <Self as Typed>::TYPE.check(&value)?;287 match value {288 Val::Arr(a) => Ok(Self(a.evaluated()?.to_vec())),289 _ => unreachable!(),290 }291 }292}293impl TryFrom<VecVal> for Val {294 type Error = LocError;295296 fn try_from(value: VecVal) -> Result<Self> {297 Ok(Self::Arr(value.0.into()))298 }299}300301/// Specialization302pub struct Bytes(pub Rc<[u8]>);303304impl Typed for Bytes {305 const TYPE: &'static ComplexValType =306 &ComplexValType::ArrayRef(&ComplexValType::BoundedNumber(Some(0.0), Some(255.0)));307}308impl TryFrom<Val> for Bytes {309 type Error = LocError;310311 fn try_from(value: Val) -> Result<Self> {312 match value {313 Val::Arr(ArrValue::Bytes(bytes)) => Ok(Self(bytes)),314 _ => {315 <Self as Typed>::TYPE.check(&value)?;316 match value {317 Val::Arr(a) => {318 let mut out = Vec::with_capacity(a.len());319 for e in a.iter() {320 let r = e?;321 out.push(u8::try_from(r)?);322 }323 Ok(Self(out.into()))324 }325 _ => unreachable!(),326 }327 }328 }329 }330}331impl TryFrom<Bytes> for Val {332 type Error = LocError;333334 fn try_from(value: Bytes) -> Result<Self> {335 Ok(Val::Arr(ArrValue::Bytes(value.0)))336 }337}338339pub struct M1;340impl Typed for M1 {341 const TYPE: &'static ComplexValType = &ComplexValType::BoundedNumber(Some(-1.0), Some(-1.0));342}343impl TryFrom<Val> for M1 {344 type Error = LocError;345346 fn try_from(value: Val) -> Result<Self> {347 <Self as Typed>::TYPE.check(&value)?;348 Ok(Self)349 }350}351impl TryFrom<M1> for Val {352 type Error = LocError;353354 fn try_from(_: M1) -> Result<Self> {355 Ok(Self::Num(-1.0))356 }357}358359macro_rules! decl_either {360 ($($name: ident, $($id: ident)*);*) => {$(361 pub enum $name<$($id),*> {362 $($id($id)),*363 }364 impl<$($id),*> Typed for $name<$($id),*>365 where366 $($id: Typed,)*367 {368 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[$($id::TYPE),*]);369 }370 impl<$($id),*> TryFrom<Val> for $name<$($id),*>371 where372 $($id: Typed,)*373 {374 type Error = LocError;375376 fn try_from(value: Val) -> Result<Self> {377 $(378 if $id::TYPE.check(&value).is_ok() {379 $id::try_from(value).map(Self::$id)380 } else381 )* {382 <Self as Typed>::TYPE.check(&value)?;383 unreachable!()384 }385 }386 }387 impl<$($id),*> TryFrom<$name<$($id),*>> for Val388 where389 $($id: Typed,)*390 {391 type Error = LocError;392 fn try_from(value: $name<$($id),*>) -> Result<Self> {393 match value {$(394 $name::$id(v) => v.try_into()395 ),*}396 }397 }398 )*}399}400decl_either!(401 Either1, A;402 Either2, A B;403 Either3, A B C;404 Either4, A B C D;405 Either5, A B C D E;406 Either6, A B C D E F;407 Either7, A B C D E F G408);409#[macro_export]410macro_rules! Either {411 ($a:ty) => {Either1<$a>};412 ($a:ty, $b:ty) => {Either2<$a, $b>};413 ($a:ty, $b:ty, $c:ty) => {Either3<$a, $b, $c>};414 ($a:ty, $b:ty, $c:ty, $d:ty) => {Either4<$a, $b, $c, $d>};415 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty) => {Either5<$a, $b, $c, $d, $e>};416 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty) => {Either6<$a, $b, $c, $d, $e, $f>};417 ($a:ty, $b:ty, $c:ty, $d:ty, $e:ty, $f:ty, $g:ty) => {Either7<$a, $b, $c, $d, $e, $f, $g>};418}419420pub type MyType = Either![u32, f64, String];421422impl Typed for ArrValue {423 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Arr);424}425impl TryFrom<Val> for ArrValue {426 type Error = LocError;427428 fn try_from(value: Val) -> Result<Self> {429 <Self as Typed>::TYPE.check(&value)?;430 match value {431 Val::Arr(a) => Ok(a),432 _ => unreachable!(),433 }434 }435}436impl TryFrom<ArrValue> for Val {437 type Error = LocError;438439 fn try_from(value: ArrValue) -> Result<Self> {440 Ok(Self::Arr(value))441 }442}443444impl Typed for Cc<FuncVal> {445 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Func);446}447impl TryFrom<Val> for Cc<FuncVal> {448 type Error = LocError;449450 fn try_from(value: Val) -> Result<Self> {451 <Self as Typed>::TYPE.check(&value)?;452 match value {453 Val::Func(a) => Ok(a),454 _ => unreachable!(),455 }456 }457}458impl TryFrom<Cc<FuncVal>> for Val {459 type Error = LocError;460461 fn try_from(value: Cc<FuncVal>) -> Result<Self> {462 Ok(Self::Func(value))463 }464}465impl Typed for ObjValue {466 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Obj);467}468impl TryFrom<Val> for ObjValue {469 type Error = LocError;470471 fn try_from(value: Val) -> Result<Self> {472 <Self as Typed>::TYPE.check(&value)?;473 match value {474 Val::Obj(a) => Ok(a),475 _ => unreachable!(),476 }477 }478}479impl TryFrom<ObjValue> for Val {480 type Error = LocError;481482 fn try_from(value: ObjValue) -> Result<Self> {483 Ok(Self::Obj(value))484 }485}486487impl Typed for bool {488 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Bool);489}490impl TryFrom<Val> for bool {491 type Error = LocError;492493 fn try_from(value: Val) -> Result<Self> {494 <Self as Typed>::TYPE.check(&value)?;495 match value {496 Val::Bool(a) => Ok(a),497 _ => unreachable!(),498 }499 }500}501impl TryFrom<bool> for Val {502 type Error = LocError;503504 fn try_from(value: bool) -> Result<Self> {505 Ok(Self::Bool(value))506 }507}508509impl Typed for IndexableVal {510 const TYPE: &'static ComplexValType = &ComplexValType::UnionRef(&[511 &ComplexValType::Simple(ValType::Arr),512 &ComplexValType::Simple(ValType::Str),513 ]);514}515impl TryFrom<Val> for IndexableVal {516 type Error = LocError;517518 fn try_from(value: Val) -> Result<Self> {519 <Self as Typed>::TYPE.check(&value)?;520 value.into_indexable()521 }522}523impl TryFrom<IndexableVal> for Val {524 type Error = LocError;525526 fn try_from(value: IndexableVal) -> Result<Self> {527 match value {528 IndexableVal::Str(s) => Ok(Self::Str(s)),529 IndexableVal::Arr(a) => Ok(Self::Arr(a)),530 }531 }532}533534pub struct Null;535impl Typed for Null {536 const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Null);537}538impl TryFrom<Val> for Null {539 type Error = LocError;540541 fn try_from(value: Val) -> Result<Self> {542 <Self as Typed>::TYPE.check(&value)?;543 Ok(Self)544 }545}546impl TryFrom<Null> for Val {547 type Error = LocError;548549 fn try_from(_: Null) -> Result<Self> {550 Ok(Self::Null)551 }552}