Skip to content

Commit

Permalink
[values][builtins] Add immutable parameter to bytes building functi…
Browse files Browse the repository at this point in the history
…ons [#68]
  • Loading branch information
cipriancraciun committed Jun 9, 2018
1 parent fd8609e commit a4393aa
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 115 deletions.
92 changes: 46 additions & 46 deletions sources/builtins_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,156 +67,156 @@ pub fn bytes_at_set (bytes : &Value, index : usize, byte : &Value) -> (Outcome<V


#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_collect_bytes <Source> (bytes : Source) -> (Value)
pub fn bytes_collect_bytes <Source> (bytes : Source, immutable : Option<bool>) -> (Value)
where Source : iter::IntoIterator<Item = u8>, Source::IntoIter : iter::DoubleEndedIterator
{
return bytes_new (iter::FromIterator::from_iter (bytes)) .into ();
return bytes_new (iter::FromIterator::from_iter (bytes), immutable) .into ();
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_collect_values <Source> (bytes : Source) -> (Outcome<Value>)
pub fn bytes_collect_values <Source> (bytes : Source, immutable : Option<bool>) -> (Outcome<Value>)
where Source : iter::IntoIterator<Item = Value>, Source::IntoIter : iter::DoubleEndedIterator, Source::IntoIter : iter::ExactSizeIterator
{
let bytes = bytes.into_iter ();
let mut buffer = StdVec::with_capacity (bytes.len ());
for byte in bytes {
buffer.push (try! (try_into_number_integer! (byte) .try_to_u8 ()));
}
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_collect_values_ref <Source, ValueRef> (bytes : Source) -> (Outcome<Value>)
pub fn bytes_collect_values_ref <Source, ValueRef> (bytes : Source, immutable : Option<bool>) -> (Outcome<Value>)
where Source : iter::IntoIterator<Item = ValueRef>, Source::IntoIter : iter::DoubleEndedIterator, Source::IntoIter : iter::ExactSizeIterator, ValueRef : StdAsRef<Value>
{
let bytes = bytes.into_iter ();
let mut buffer = StdVec::with_capacity (bytes.len ());
for byte in bytes {
buffer.push (try! (try_as_number_integer_ref! (byte.as_ref ()) .try_to_u8 ()));
}
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}




#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_collect_bytes_from_generator <Source> (bytes : Source) -> (Outcome<Value>)
pub fn bytes_collect_bytes_from_generator <Source> (bytes : Source, immutable : Option<bool>) -> (Outcome<Value>)
where Source : iter::Iterator<Item = Outcome<u8>>
{
TODO! ("eliminate vector allocation");
let bytes = try! (bytes.collect::<Outcome<StdVec<_>>> ());
succeed! (bytes_collect_bytes (bytes));
succeed! (bytes_collect_bytes (bytes, immutable));
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_collect_values_from_generator <Source> (bytes : Source) -> (Outcome<Value>)
pub fn bytes_collect_values_from_generator <Source> (bytes : Source, immutable : Option<bool>) -> (Outcome<Value>)
where Source : iter::Iterator<Item = Outcome<Value>>
{
TODO! ("eliminate vector allocation");
let bytes = try! (bytes.collect::<Outcome<StdVec<_>>> ());
return bytes_collect_values (bytes);
return bytes_collect_values (bytes, immutable);
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_collect_values_from_generator_ref <Source, ValueRef> (bytes : Source) -> (Outcome<Value>)
pub fn bytes_collect_values_from_generator_ref <Source, ValueRef> (bytes : Source, immutable : Option<bool>) -> (Outcome<Value>)
where Source : iter::Iterator<Item = Outcome<ValueRef>>, ValueRef : StdAsRef<Value>
{
TODO! ("eliminate vector allocation");
let bytes = try! (bytes.collect::<Outcome<StdVec<_>>> ());
return bytes_collect_values_ref (bytes);
return bytes_collect_values_ref (bytes, immutable);
}




#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_empty () -> (Value) {
return bytes_new_empty () .into ();
pub fn bytes_empty (immutable : Option<bool>) -> (Value) {
return bytes_new_empty (immutable) .into ();
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_build_1 (byte_1 : &Value) -> (Outcome<Value>) {
pub fn bytes_build_1 (byte_1 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
let mut buffer = StdVec::with_capacity (1);
buffer.push (try! (try_as_number_integer_ref! (byte_1) .try_to_u8 ()));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_build_2 (byte_1 : &Value, byte_2 : &Value) -> (Outcome<Value>) {
pub fn bytes_build_2 (byte_1 : &Value, byte_2 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
let mut buffer = StdVec::with_capacity (2);
buffer.push (try! (try_as_number_integer_ref! (byte_1) .try_to_u8 ()));
buffer.push (try! (try_as_number_integer_ref! (byte_2) .try_to_u8 ()));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_build_3 (byte_1 : &Value, byte_2 : &Value, byte_3 : &Value) -> (Outcome<Value>) {
pub fn bytes_build_3 (byte_1 : &Value, byte_2 : &Value, byte_3 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
let mut buffer = StdVec::with_capacity (3);
buffer.push (try! (try_as_number_integer_ref! (byte_1) .try_to_u8 ()));
buffer.push (try! (try_as_number_integer_ref! (byte_2) .try_to_u8 ()));
buffer.push (try! (try_as_number_integer_ref! (byte_3) .try_to_u8 ()));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_build_4 (byte_1 : &Value, byte_2 : &Value, byte_3 : &Value, byte_4 : &Value) -> (Outcome<Value>) {
pub fn bytes_build_4 (byte_1 : &Value, byte_2 : &Value, byte_3 : &Value, byte_4 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
let mut buffer = StdVec::with_capacity (4);
buffer.push (try! (try_as_number_integer_ref! (byte_1) .try_to_u8 ()));
buffer.push (try! (try_as_number_integer_ref! (byte_2) .try_to_u8 ()));
buffer.push (try! (try_as_number_integer_ref! (byte_3) .try_to_u8 ()));
buffer.push (try! (try_as_number_integer_ref! (byte_4) .try_to_u8 ()));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_build_n (bytes : &[impl StdAsRef<Value>]) -> (Outcome<Value>) {
pub fn bytes_build_n (bytes : &[impl StdAsRef<Value>], immutable : Option<bool>) -> (Outcome<Value>) {
if bytes.is_empty () {
succeed! (bytes_empty ());
succeed! (bytes_empty (immutable));
}
let mut buffer = StdVec::with_capacity (bytes.len ());
for byte in bytes {
let byte = byte.as_ref ();
buffer.push (try! (try_as_number_integer_ref! (byte) .try_to_u8 ()));
}
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}




#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_append_2 (bytes_1 : &Value, bytes_2 : &Value) -> (Outcome<Value>) {
pub fn bytes_append_2 (bytes_1 : &Value, bytes_2 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
let buffer = try! (vec_bytes_append_2 (bytes_1, bytes_2));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_append_3 (bytes_1 : &Value, bytes_2 : &Value, bytes_3 : &Value) -> (Outcome<Value>) {
pub fn bytes_append_3 (bytes_1 : &Value, bytes_2 : &Value, bytes_3 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
let buffer = try! (vec_bytes_append_3 (bytes_1, bytes_2, bytes_3));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_append_4 (bytes_1 : &Value, bytes_2 : &Value, bytes_3 : &Value, bytes_4 : &Value) -> (Outcome<Value>) {
pub fn bytes_append_4 (bytes_1 : &Value, bytes_2 : &Value, bytes_3 : &Value, bytes_4 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
let buffer = try! (vec_bytes_append_4 (bytes_1, bytes_2, bytes_3, bytes_4));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_append_n (bytes : &[impl StdAsRef<Value>]) -> (Outcome<Value>) {
pub fn bytes_append_n (bytes : &[impl StdAsRef<Value>], immutable : Option<bool>) -> (Outcome<Value>) {
if bytes.is_empty () {
succeed! (bytes_empty ());
succeed! (bytes_empty (immutable));
}
let buffer = try! (vec_bytes_append_n (bytes));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}




#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_make (length : usize, fill : Option<&Value>) -> (Outcome<Value>) {
pub fn bytes_make (length : usize, fill : Option<&Value>, immutable : Option<bool>) -> (Outcome<Value>) {
let fill = if let Some (fill) = fill {
try! (try_as_number_integer_ref! (fill) .try_to_u8 ())
} else {
Expand All @@ -226,20 +226,20 @@ pub fn bytes_make (length : usize, fill : Option<&Value>) -> (Outcome<Value>) {
for _index in 0..length {
buffer.push (fill);
}
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_clone (bytes : &Value) -> (Outcome<Value>) {
pub fn bytes_clone (bytes : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
let buffer = try! (vec_bytes_clone (bytes));
succeed! (bytes_new (buffer) .into ());
succeed! (bytes_new (buffer, immutable) .into ());
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_reverse (bytes : &Value) -> (Outcome<Value>) {
pub fn bytes_reverse (bytes : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
TODO! ("optimize the vector allocation");
let buffer = try! (vec_bytes_clone (bytes));
succeed! (bytes_collect_bytes (buffer.into_iter () .rev ()));
succeed! (bytes_collect_bytes (buffer.into_iter () .rev (), immutable));
}


Expand Down Expand Up @@ -298,10 +298,10 @@ pub fn bytes_copy_range (target_bytes : &Value, target_start : Option<&Value>, s


#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_clone_range (bytes : &Value, range_start : Option<&Value>, range_end : Option<&Value>) -> (Outcome<Value>) {
pub fn bytes_clone_range (bytes : &Value, range_start : Option<&Value>, range_end : Option<&Value>, immutable : Option<bool>) -> (Outcome<Value>) {
let bytes = try_as_bytes_ref! (bytes);
let (range_start, range_end) = try! (range_coerce (range_start, range_end, bytes.bytes_count ()));
succeed! (bytes_clone_slice (& bytes.bytes_as_slice () [range_start..range_end]) .into ());
succeed! (bytes_clone_slice (& bytes.bytes_as_slice () [range_start..range_end], immutable) .into ());
}


Expand All @@ -314,9 +314,9 @@ pub fn bytes_range_to_list (bytes : &Value, range_start : Option<&Value>, range_
}

#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn list_range_to_bytes (list : &Value, range_start : Option<&Value>, range_end : Option<&Value>) -> (Outcome<Value>) {
pub fn list_range_to_bytes (list : &Value, range_start : Option<&Value>, range_end : Option<&Value>, immutable : Option<bool>) -> (Outcome<Value>) {
let iterator = try! (list_range_iterator (list, range_start, range_end));
return bytes_collect_values_from_generator_ref (iterator);
return bytes_collect_values_from_generator_ref (iterator, immutable);
}


Expand All @@ -329,9 +329,9 @@ pub fn bytes_range_to_array (bytes : &Value, range_start : Option<&Value>, range

#[ cfg ( feature = "vonuvoli_values_array" ) ]
#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn array_range_to_bytes (array : &Value, range_start : Option<&Value>, range_end : Option<&Value>) -> (Outcome<Value>) {
pub fn array_range_to_bytes (array : &Value, range_start : Option<&Value>, range_end : Option<&Value>, immutable : Option<bool>) -> (Outcome<Value>) {
let iterator = try! (array_range_iterator (array, range_start, range_end));
return bytes_collect_values_from_generator_ref (iterator);
return bytes_collect_values_from_generator_ref (iterator, immutable);
}


Expand Down
4 changes: 2 additions & 2 deletions sources/builtins_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ pub fn cache_select_bytes (cache : &Value, namespace : Option<&Value>, key : &Va
let busting = option_map! (busting, ext::blake2_rfc::blake2b::blake2b (CACHE_BUSTING_SIZE, partition_key.unwrap_or (&[]), busting));
let busting = option_ref_map! (busting, busting.as_bytes ());

let value = try! (cache_backend_select (database, key, time_to_live, busting, integrity_key, |value| succeed! (bytes_clone_slice (value))));
let value = try! (cache_backend_select (database, key, time_to_live, busting, integrity_key, |value| succeed! (bytes_clone_slice (value, None))));
let value = value.unwrap_or (FALSE_VALUE);

succeed! (value);
Expand Down Expand Up @@ -563,7 +563,7 @@ pub fn cache_resolve_bytes (cache : &Value, namespace : Option<&Value>, key : &V
let busting = option_ref_map! (busting, busting.as_bytes ());

{
let value = try! (cache_backend_select (database, key, time_to_live, busting, integrity_key, |value| succeed! (bytes_clone_slice (value))));
let value = try! (cache_backend_select (database, key, time_to_live, busting, integrity_key, |value| succeed! (bytes_clone_slice (value, None))));
if let Some (value) = value {
succeed! (value);
}
Expand Down
28 changes: 14 additions & 14 deletions sources/builtins_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,13 +579,13 @@ pub fn arrays_iterate_n (evaluator : &mut EvaluatorContext, callable : &Value, a

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_map_1 (evaluator : &mut EvaluatorContext, callable : &Value, bytes : &Value) -> (Outcome<Value>) {
pub fn bytes_map_1 (evaluator : &mut EvaluatorContext, callable : &Value, bytes : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
if try! (is_bytes_empty (bytes)) {
succeed! (bytes_empty ());
succeed! (bytes_empty (immutable));
}
let iterator = try! (BytesIterator::new (bytes));
let outputs = try! (iterators_map_1 (evaluator, callable, iterator));
return bytes_collect_values (outputs);
return bytes_collect_values (outputs, immutable);
}

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
Expand All @@ -602,14 +602,14 @@ pub fn bytes_iterate_1 (evaluator : &mut EvaluatorContext, callable : &Value, by

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_map_2 (evaluator : &mut EvaluatorContext, callable : &Value, bytes_1 : &Value, bytes_2 : &Value) -> (Outcome<Value>) {
pub fn bytes_map_2 (evaluator : &mut EvaluatorContext, callable : &Value, bytes_1 : &Value, bytes_2 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
if try! (is_bytes_empty_all_2 (bytes_1, bytes_2)) {
succeed! (bytes_empty ());
succeed! (bytes_empty (immutable));
}
let iterator_1 = try! (BytesIterator::new (bytes_1));
let iterator_2 = try! (BytesIterator::new (bytes_2));
let outputs = try! (iterators_map_2 (evaluator, callable, iterator_1, iterator_2));
return bytes_collect_values (outputs);
return bytes_collect_values (outputs, immutable);
}

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
Expand All @@ -627,15 +627,15 @@ pub fn bytes_iterate_2 (evaluator : &mut EvaluatorContext, callable : &Value, by

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_map_3 (evaluator : &mut EvaluatorContext, callable : &Value, bytes_1 : &Value, bytes_2 : &Value, bytes_3 : &Value) -> (Outcome<Value>) {
pub fn bytes_map_3 (evaluator : &mut EvaluatorContext, callable : &Value, bytes_1 : &Value, bytes_2 : &Value, bytes_3 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
if try! (is_bytes_empty_all_3 (bytes_1, bytes_2, bytes_3)) {
succeed! (bytes_empty ());
succeed! (bytes_empty (immutable));
}
let iterator_1 = try! (BytesIterator::new (bytes_1));
let iterator_2 = try! (BytesIterator::new (bytes_2));
let iterator_3 = try! (BytesIterator::new (bytes_3));
let outputs = try! (iterators_map_3 (evaluator, callable, iterator_1, iterator_2, iterator_3));
return bytes_collect_values (outputs);
return bytes_collect_values (outputs, immutable);
}

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
Expand All @@ -654,16 +654,16 @@ pub fn bytes_iterate_3 (evaluator : &mut EvaluatorContext, callable : &Value, by

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_map_4 (evaluator : &mut EvaluatorContext, callable : &Value, bytes_1 : &Value, bytes_2 : &Value, bytes_3 : &Value, bytes_4 : &Value) -> (Outcome<Value>) {
pub fn bytes_map_4 (evaluator : &mut EvaluatorContext, callable : &Value, bytes_1 : &Value, bytes_2 : &Value, bytes_3 : &Value, bytes_4 : &Value, immutable : Option<bool>) -> (Outcome<Value>) {
if try! (is_bytes_empty_all_4 (bytes_1, bytes_2, bytes_3, bytes_4)) {
succeed! (bytes_empty ());
succeed! (bytes_empty (immutable));
}
let iterator_1 = try! (BytesIterator::new (bytes_1));
let iterator_2 = try! (BytesIterator::new (bytes_2));
let iterator_3 = try! (BytesIterator::new (bytes_3));
let iterator_4 = try! (BytesIterator::new (bytes_4));
let outputs = try! (iterators_map_4 (evaluator, callable, iterator_1, iterator_2, iterator_3, iterator_4));
return bytes_collect_values (outputs);
return bytes_collect_values (outputs, immutable);
}

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
Expand All @@ -683,13 +683,13 @@ pub fn bytes_iterate_4 (evaluator : &mut EvaluatorContext, callable : &Value, by

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
#[ cfg_attr ( feature = "vonuvoli_inline", inline ) ]
pub fn bytes_map_n (evaluator : &mut EvaluatorContext, callable : &Value, bytes : &[impl StdAsRef<Value>]) -> (Outcome<Value>) {
pub fn bytes_map_n (evaluator : &mut EvaluatorContext, callable : &Value, bytes : &[impl StdAsRef<Value>], immutable : Option<bool>) -> (Outcome<Value>) {
if bytes.is_empty () {
fail! (0xfa789f5a);
}
let iterators = try! (BytesIterators::new (bytes));
let outputs = try! (iterators_map_n (evaluator, callable, iterators));
return bytes_collect_values (outputs);
return bytes_collect_values (outputs, immutable);
}

#[ cfg ( feature = "vonuvoli_values_bytes" ) ]
Expand Down
Loading

0 comments on commit a4393aa

Please sign in to comment.