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
97
98
use core::{cmp, fmt};
use crate::internal::fuse_io;
#[derive(Hash)]
#[repr(transparent)]
pub struct NodeName([u8]);
#[rustfmt::skip]
pub const NODE_NAME_MAX: usize = {
	#[cfg(target_os = "linux")]   { 255 }
	#[cfg(target_os = "freebsd")] { 255 }
};
impl NodeName {
	pub(crate) fn new<'a>(
		bytes: fuse_io::NulTerminatedBytes<'a>,
	) -> &'a NodeName {
		let bytes = bytes.to_bytes_without_nul();
		unsafe { &*(bytes as *const [u8] as *const NodeName) }
	}
	pub fn from_bytes<'a>(bytes: &'a [u8]) -> Option<&'a NodeName> {
		let len = bytes.len();
		if len == 0 || len > NODE_NAME_MAX {
			return None;
		}
		if bytes.contains(&0) || bytes.contains(&b'/') {
			return None;
		}
		Some(unsafe { &*(bytes as *const [u8] as *const NodeName) })
	}
	pub fn as_bytes(&self) -> &[u8] {
		&self.0
	}
}
impl fmt::Debug for NodeName {
	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
		fmt::Display::fmt(self, fmt)
	}
}
impl fmt::Display for NodeName {
	fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
		use core::fmt::Debug;
		super::DebugBytesAsString(&self.0).fmt(fmt)
	}
}
impl Eq for NodeName {}
impl PartialEq for NodeName {
	fn eq(&self, other: &NodeName) -> bool {
		self.as_bytes().eq(other.as_bytes())
	}
}
impl PartialEq<[u8]> for NodeName {
	fn eq(&self, other: &[u8]) -> bool {
		self.as_bytes().eq(other)
	}
}
impl Ord for NodeName {
	fn cmp(&self, other: &NodeName) -> cmp::Ordering {
		self.as_bytes().cmp(&other.as_bytes())
	}
}
impl PartialEq<NodeName> for [u8] {
	fn eq(&self, other: &NodeName) -> bool {
		self.eq(other.as_bytes())
	}
}
impl PartialOrd for NodeName {
	fn partial_cmp(&self, other: &NodeName) -> Option<cmp::Ordering> {
		self.as_bytes().partial_cmp(&other.as_bytes())
	}
}