Repository: vercel/next.js
File: turbopack/crates/turbo-tasks-fs/src/rope.rs (Rust)

Code before:
            Uncommitted::Static(s) => f
                .debug_tuple("Static")
                .field(&Bytes::from_static(s))
                .finish(),
            Uncommitted::Owned(v) => f
                .debug_tuple("Owned")
                .field(&Bytes::from(v.clone()))
                .finish(),
        }
    }
}

impl DeterministicHash for Rope {
    /// Ropes with similar contents hash the same, regardless of their
    /// structure.
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        state.write_usize(self.len());
        self.data.deterministic_hash(state);
    }
}

impl Rope {
    /// Returns a DeterministicHash impl that only hashes the bytes of the rope. The default
    /// DeterministicHash implementation also includes the length of the rope. Be careful when using
    /// this, as it would case `(Rope("abc"), Rope("def"))` and `(Rope("abcd"), Rope("ef"))` to have
    /// the same hash.
    pub fn content_hash(&self) -> impl DeterministicHash + '_ {
        RopeBytesOnlyHash(self)
    }
}
pub struct RopeBytesOnlyHash<'a>(&'a Rope);
impl DeterministicHash for RopeBytesOnlyHash<'_> {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        self.0.data.deterministic_hash(state);
    }
}

/// Encode as a len + raw bytes format using the encoder's [`bincode::enc::write::Writer`]. Encoding
/// [`Rope::to_bytes`] instead would be easier, but would require copying to an intermediate buffer.
///
/// This len + bytes format is similar to how bincode would normally encode a `&[u8]`:
/// https://docs.rs/bincode/latest/bincode/spec/index.html#collections
impl Encode for Rope {
    fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
        self.length.encode(encoder)?;
        let mut reader = self.read();
        for chunk in &mut reader {
            encoder.writer().write(chunk)?;
        }

        Ok(())

Review comment (suggestion):
```suggestion
    /// Returns a DeterministicHash impl that only hashes the bytes of the rope. The default
    /// DeterministicHash implementation also includes the length of the rope. Be careful when using
    /// this, as it would case `(Rope("abc"), Rope("def"))` and `(Rope("abcd"), Rope("ef"))` to have
    /// the same hash.  The best usecase is when the rope is the _whole_ datastructure being hashed and it isn't part of some other structure.
```

Code after:
            Uncommitted::Static(s) => f
                .debug_tuple("Static")
                .field(&Bytes::from_static(s))
                .finish(),
            Uncommitted::Owned(v) => f
                .debug_tuple("Owned")
                .field(&Bytes::from(v.clone()))
                .finish(),
        }
    }
}

impl DeterministicHash for Rope {
    /// Ropes with similar contents hash the same, regardless of their
    /// structure.
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        state.write_usize(self.len());
        self.data.deterministic_hash(state);
    }
}

impl Rope {
    /// Returns a DeterministicHash impl that only hashes the bytes of the rope (still regardless of
    /// their structure).
    ///
    /// The default (Deterministic)Hash implementation also includes the length of the rope. Be
    /// careful when using this, as it would case `(Rope("abc"), Rope("def"))` and `(Rope("abcd"),
    /// Rope("ef"))` to have the same hash. The best usecase is when the rope is the _whole_
    /// datastructure being hashed and it isn't part of some other structure.
    pub fn content_hash(&self) -> impl DeterministicHash + '_ {
        RopeBytesOnlyHash(self)
    }
}
pub struct RopeBytesOnlyHash<'a>(&'a Rope);
impl DeterministicHash for RopeBytesOnlyHash<'_> {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        self.0.data.deterministic_hash(state);
    }
}

/// Encode as a len + raw bytes format using the encoder's [`bincode::enc::write::Writer`]. Encoding
/// [`Rope::to_bytes`] instead would be easier, but would require copying to an intermediate buffer.
///
/// This len + bytes format is similar to how bincode would normally encode a `&[u8]`:
/// https://docs.rs/bincode/latest/bincode/spec/index.html#collections
impl Encode for Rope {
    fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
        self.length.encode(encoder)?;
        let mut reader = self.read();
        for chunk in &mut reader {
            encoder.writer().write(chunk)?;