Repository: paritytech/substrate
File: frame/election-provider-multi-phase/src/signed.rs (Rust)

Code before:
	pub fn decode_len() -> Option<usize> {
		SignedSubmissionIndices::<T>::decode_len()
	}

	/// Insert a new signed submission into the set.
	///
	/// In the event that the new submission is not better than the current weakest according
	/// to `is_score_better`, we do not change anything.
	pub fn insert(&mut self, submission: SignedSubmissionOf<T>) -> InsertResult<T> {
		// verify the expectation that we never reuse an index
		debug_assert!(!self.indices.iter().map(|(_, _, x)| x).any(|&idx| idx == self.next_idx));
		let block_number = frame_system::Pallet::<T>::block_number();

		let weakest = match self.indices.try_push((
			submission.raw_solution.score,
			block_number,
			self.next_idx,
		)) {
			Ok(_) => None,
			Err(_) => {
				// the queue is full -- if this is better, insert it.
				let weakest_score = match self.indices.iter().next().defensive() {
					None => return InsertResult::NotInserted,
					Some((score, _, _)) => *score,
				};
				let threshold = T::BetterSignedThreshold::get();

				// if we haven't improved on the weakest score, don't change anything.
				if !submission.raw_solution.score.strict_threshold_better(weakest_score, threshold)
				{
					return InsertResult::NotInserted
				}

				self.swap_out_submission(
					0, // swap out the worse one, which is always index 0.
					Some((submission.raw_solution.score, block_number, self.next_idx)),
				)
			},
		};

		// this is the ONLY place that we insert, and we sort post insertion. If scores are the
		// same, we sort based on reverse of submission block number.
		self.indices
			.sort_by(|(score1, bn1, _), (score2, bn2, _)| match score1.cmp(score2) {
				Ordering::Equal => bn1.cmp(&bn2).reverse(),
				x @ _ => x,
			});

		// we've taken out the weakest, so update the storage map and the next index
		debug_assert!(!self.insertion_overlay.contains_key(&self.next_idx));
		self.insertion_overlay.insert(self.next_idx, submission);

Review comment (suggestion):
I think it would make the match a little bit more readable to replace the `Option` with `InsertResult` directly below.

Code after:
	}

	/// Decode the length of the signed submissions without actually reading the entire struct into
	/// memory.
	///
	/// Note that if you hold an instance of `SignedSubmissions`, this function does _not_
	/// track its current length. This only decodes what is currently stored in memory.
	pub fn decode_len() -> Option<usize> {
		SignedSubmissionIndices::<T>::decode_len()
	}

	/// Insert a new signed submission into the set.
	///
	/// In the event that the new submission is not better than the current weakest according
	/// to `is_score_better`, we do not change anything.
	pub fn insert(&mut self, submission: SignedSubmissionOf<T>) -> InsertResult<T> {
		// verify the expectation that we never reuse an index
		debug_assert!(!self.indices.iter().map(|(_, _, x)| x).any(|&idx| idx == self.next_idx));
		let block_number = frame_system::Pallet::<T>::block_number();

		let maybe_weakest = match self.indices.try_push((
			submission.raw_solution.score,
			block_number,
			self.next_idx,
		)) {
			Ok(_) => None,
			Err(_) => {
				// the queue is full -- if this is better, insert it.
				let weakest_score = match self.indices.iter().next().defensive() {
					None => return InsertResult::NotInserted,
					Some((score, _, _)) => *score,
				};
				let threshold = T::BetterSignedThreshold::get();

				// if we haven't improved on the weakest score, don't change anything.
				if !submission.raw_solution.score.strict_threshold_better(weakest_score, threshold)
				{
					return InsertResult::NotInserted
				}

				self.swap_out_submission(
					0, // swap out the worse one, which is always index 0.
					Some((submission.raw_solution.score, block_number, self.next_idx)),
				)
			},
		};

		// this is the ONLY place that we insert, and we sort post insertion. If scores are the
		// same, we sort based on reverse of submission block number.
		self.indices
			.sort_by(|(score1, bn1, _), (score2, bn2, _)| match score1.cmp(score2) {