Repository: tikv/tikv
File: components/txn_types/src/lock.rs (Rust)

Code before:
                    *either = Either::Right(lock);
                    either.as_ref().right()
                }
                Either::Right(lock) => Some(lock),
            }
        } else {
            None
        }
    }

    fn put_lock(&mut self, ts: TimeStamp, lock: Lock) -> Option<Lock> {
        let previous = self.txn_info_segments.insert(ts, Either::Right(lock));

        match previous {
            Some(either) => match either {
                Either::Left(encoded) => {
                    // Previously stored as encoded lock info; decode it back to a Lock.
                    Some(Lock::parse(&encoded).expect("failed to parse shared lock txn info"))
                }
                Either::Right(lock) => Some(lock),
            },
            None => None,
        }
    }

    #[inline]
    pub fn put_shared_lock(&mut self, lock: Lock) -> Result<()> {
        let lock_type = lock.lock_type;
        match lock_type {
            LockType::Lock | LockType::Pessimistic => {}
            _ => unreachable!(),
        }
        if self.is_shrink_only() {
            return Err(Error::from(ErrorInner::SharedLocksAreShrinkOnly));
        }
        let ts = lock.ts;
        let old = self.put_lock(ts, lock);
        if lock_type == LockType::Lock {
            debug_assert!(
                old.is_some(),
                "shared lock should be prewritten over pessimistic lock"
            );
        }
        Ok(())
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let mut b = Vec::with_capacity(self.pre_allocate_size());
        b.push(LockType::Shared.to_u8());
        if !self.flags.is_empty() {
            b.push(SHARED_LOCK_FLAGS_PREFIX);

Review comment (bug):
The method signature has changed to return a Result, but there is no documentation explaining when this method can fail. Consider adding a doc comment that explains the method can return an error when attempting to add a lock to a shrink-only SharedLocks instance.
```suggestion
    #[inline]
    /// Inserts a shared lock into this `SharedLocks` collection.
    ///
    /// # Errors
    ///
    /// Returns an error if this `SharedLocks` instance is marked as shrink-only
    /// and cannot accept new locks, in which case `ErrorInner::SharedLocksAreShrinkOnly`
    /// is returned.
```

Code after:
                    *either = Either::Right(lock);
                    either.as_ref().right()
                }
                Either::Right(lock) => Some(lock),
            }
        } else {
            None
        }
    }

    pub fn insert_lock(&mut self, lock: Lock) -> Result<()> {
        let lock_type = lock.lock_type;
        assert!(matches!(lock_type, LockType::Lock | LockType::Pessimistic));
        if self.is_shrink_only() {
            return Err(ErrorInner::InvalidOperation(format!(
                "cannot insert new lock #{} into shrink-only SharedLocks",
                lock.ts.into_inner()
            ))
            .into());
        } else if self.contains_start_ts(lock.ts) {
            return Err(ErrorInner::InvalidOperation(format!(
                "lock #{} already exists in SharedLocks",
                lock.ts.into_inner()
            ))
            .into());
        }
        self.txn_info_segments.insert(lock.ts, Either::Right(lock));
        Ok(())
    }

    pub fn update_lock(&mut self, lock: Lock) -> Result<()> {
        let lock_type = lock.lock_type;
        assert!(matches!(lock_type, LockType::Lock | LockType::Pessimistic));
        if !self.contains_start_ts(lock.ts) {
            return Err(ErrorInner::InvalidOperation(format!(
                "lock #{} does not exist in SharedLocks",
                lock.ts.into_inner()
            ))
            .into());
        }
        self.txn_info_segments.insert(lock.ts, Either::Right(lock));
        Ok(())
    }

    pub fn to_bytes(&self) -> Vec<u8> {
        let mut b = Vec::with_capacity(self.pre_allocate_size());
        b.push(LockType::Shared.to_u8());
        if !self.flags.is_empty() {
            b.push(SHARED_LOCK_FLAGS_PREFIX);
            b.push(self.flags.bits());
        }