-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathvariable.rs
More file actions
96 lines (79 loc) · 2.24 KB
/
Copy pathvariable.rs
File metadata and controls
96 lines (79 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors
use std::fmt;
use std::fmt::Display;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::sync::Arc;
use crate::expr::Expression;
/// The name of a value bound in a [`Scope`](crate::expr::Scope).
#[derive(Clone, Debug)]
pub struct Variable(Arc<str>);
impl Variable {
/// Create a variable with the given name.
pub fn new(name: impl AsRef<str>) -> Self {
Self(Arc::from(name.as_ref()))
}
/// The variable's name.
pub fn name(&self) -> &str {
&self.0
}
}
/// Compares by name, with a pointer-equality fast path for the common case of a cloned handle.
impl PartialEq for Variable {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0) || self.0 == other.0
}
}
impl Eq for Variable {}
impl Hash for Variable {
fn hash<H: Hasher>(&self, state: &mut H) {
// Hash the name, not the pointer, so equal names hash equally.
self.0.hash(state);
}
}
impl Display for Variable {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<Variable> for Expression {
fn from(variable: Variable) -> Self {
Expression::Variable(variable)
}
}
impl<T: AsRef<str>> From<T> for Variable {
fn from(name: T) -> Self {
Self::new(name)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn equality_is_by_name_not_identity() {
let x1 = Variable::new("x");
let x2 = Variable::new("x");
let y = Variable::new("y");
// Independently constructed, so no shared allocation to short-circuit on.
assert_eq!(x1, x2);
assert_ne!(x1, y);
}
#[test]
fn equal_variables_hash_equally() {
use std::collections::hash_map::RandomState;
use std::hash::BuildHasher;
let state = RandomState::new();
assert_eq!(
state.hash_one(Variable::new("x")),
state.hash_one(Variable::new("x"))
);
}
#[test]
fn cloning_shares_the_name() {
let original = Variable::new("x");
let cloned = original.clone();
assert!(Arc::ptr_eq(&original.0, &cloned.0));
}
}