runtime.rs 35 KiB
Newer Older

		match ctx.ext.get_runtime_storage(&key_buf) {
			Some(value_buf) => {
				// The given value exists.
				ctx.scratch_buf = value_buf;
				Ok(0)
			}
			None => {
				// Put back the `key_buf` and allow its allocation to be reused.
				ctx.scratch_buf = key_buf;
				ctx.scratch_buf.clear();
				Ok(1)
			}
		}
	},

/// Finds duplicates in a given vector.
///
/// This function has complexity of O(n log n) and no additional memory is required, although
/// the order of items is not preserved.
fn has_duplicates<T: PartialEq + AsRef<[u8]>>(items: &mut Vec<T>) -> bool {
	// Sort the vector
	items.sort_unstable_by(|a, b| {
		Ord::cmp(a.as_ref(), b.as_ref())
	});
	// And then find any two consecutive equal elements.
	items.windows(2).any(|w| {
		match w {
			&[ref a, ref b] => a == b,
			_ => false,
		}
	})
}