Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Eq and Hash for GreenTokenData #174

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/green/token.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
borrow::Borrow,
fmt,
hash::{Hash, Hasher},
mem::{self, ManuallyDrop},
ops, ptr,
};
Expand All @@ -21,6 +22,7 @@ struct GreenTokenHead {

type Repr = HeaderSlice<GreenTokenHead, [u8]>;
type ReprThin = HeaderSlice<GreenTokenHead, [u8; 0]>;
#[derive(Eq)]
#[repr(transparent)]
pub struct GreenTokenData {
data: ReprThin,
Expand All @@ -32,6 +34,12 @@ impl PartialEq for GreenTokenData {
}
}

impl Hash for GreenTokenData {
fn hash<H: Hasher>(&self, state: &mut H) {
(*self.data).hash(state);
}
}

/// Leaf node in the immutable tree.
#[derive(PartialEq, Eq, Hash, Clone)]
#[repr(transparent)]
Expand Down Expand Up @@ -141,3 +149,18 @@ impl ops::Deref for GreenToken {
}
}
}

#[cfg(test)]
mod tests {
use std::hash::{BuildHasher, RandomState};

use super::*;

#[test]
fn hash_borrow() {
let owned = GreenToken::new(SyntaxKind(42), "foobar");
let borrowed: &GreenTokenData = owned.borrow();
let s = RandomState::new();
assert_eq!(s.hash_one(&owned), s.hash_one(borrowed));
}
}
Loading