Skip to content

Commit 7e8718b

Browse files
committed
Put str_id macro into a separate module
For better modularity. Signed-off-by: mulhern <amulhern@redhat.com>
1 parent b25bcd0 commit 7e8718b

3 files changed

Lines changed: 86 additions & 77 deletions

File tree

src/id_macros.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// This Source Code Form is subject to the terms of the Mozilla Public
2+
// License, v. 2.0. If a copy of the MPL was not distributed with this
3+
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
// A module to contain functionality used for generating DM ids which
6+
// are restricted in length and format by devicemapper.
7+
8+
/// Define borrowed and owned versions of string types that guarantee
9+
/// conformance to DM restrictions, such as maximum length.
10+
// This implementation follows the example of Path/PathBuf as closely as
11+
// possible.
12+
macro_rules! str_id {
13+
($B:ident, $O:ident, $MAX:ident, $check:ident) => {
14+
/// The borrowed version of the DM identifier.
15+
#[derive(Debug, PartialEq, Eq, Hash)]
16+
pub struct $B {
17+
inner: str,
18+
}
19+
20+
/// The owned version of the DM identifier.
21+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
22+
pub struct $O {
23+
inner: String,
24+
}
25+
26+
impl $B {
27+
/// Create a new borrowed identifier from a `&str`.
28+
#[allow(clippy::new_ret_no_self)]
29+
pub fn new(value: &str) -> DmResult<&$B> {
30+
$check(value, $MAX - 1)?;
31+
Ok(unsafe { &*(value as *const str as *const $B) })
32+
}
33+
34+
/// Get the inner value as bytes
35+
pub fn as_bytes(&self) -> &[u8] {
36+
self.inner.as_bytes()
37+
}
38+
}
39+
40+
impl ToOwned for $B {
41+
type Owned = $O;
42+
fn to_owned(&self) -> $O {
43+
$O {
44+
inner: self.inner.to_owned(),
45+
}
46+
}
47+
}
48+
49+
impl fmt::Display for $B {
50+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
51+
write!(f, "{}", &self.inner)
52+
}
53+
}
54+
55+
impl $O {
56+
/// Construct a new owned identifier.
57+
#[allow(clippy::new_ret_no_self)]
58+
pub fn new(value: String) -> DmResult<$O> {
59+
$check(&value, $MAX - 1)?;
60+
Ok($O { inner: value })
61+
}
62+
}
63+
64+
impl AsRef<$B> for $O {
65+
fn as_ref(&self) -> &$B {
66+
self
67+
}
68+
}
69+
70+
impl Borrow<$B> for $O {
71+
fn borrow(&self) -> &$B {
72+
self.deref()
73+
}
74+
}
75+
76+
impl Deref for $O {
77+
type Target = $B;
78+
fn deref(&self) -> &$B {
79+
$B::new(&self.inner).expect("inner satisfies all correctness criteria for $B::new")
80+
}
81+
}
82+
};
83+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ extern crate uuid;
9393
/// Range macros
9494
#[macro_use]
9595
mod range_macros;
96+
/// ID macros
97+
#[macro_use]
98+
mod id_macros;
9699
/// shared constants
97100
mod consts;
98101
/// rust definitions of ioctl structs and consts

src/types.rs

Lines changed: 0 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -84,83 +84,6 @@ fn str_check(value: &str, max_allowed_chars: usize) -> DmResult<()> {
8484
Ok(())
8585
}
8686

87-
/// Define borrowed and owned versions of string types that guarantee
88-
/// conformance to DM restrictions, such as maximum length.
89-
// This implementation follows the example of Path/PathBuf as closely as
90-
// possible.
91-
macro_rules! str_id {
92-
($B:ident, $O:ident, $MAX:ident, $check:ident) => {
93-
/// The borrowed version of the DM identifier.
94-
#[derive(Debug, PartialEq, Eq, Hash)]
95-
pub struct $B {
96-
inner: str,
97-
}
98-
99-
/// The owned version of the DM identifier.
100-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
101-
pub struct $O {
102-
inner: String,
103-
}
104-
105-
impl $B {
106-
/// Create a new borrowed identifier from a `&str`.
107-
#[allow(clippy::new_ret_no_self)]
108-
pub fn new(value: &str) -> DmResult<&$B> {
109-
$check(value, $MAX - 1)?;
110-
Ok(unsafe { &*(value as *const str as *const $B) })
111-
}
112-
113-
/// Get the inner value as bytes
114-
pub fn as_bytes(&self) -> &[u8] {
115-
self.inner.as_bytes()
116-
}
117-
}
118-
119-
impl ToOwned for $B {
120-
type Owned = $O;
121-
fn to_owned(&self) -> $O {
122-
$O {
123-
inner: self.inner.to_owned(),
124-
}
125-
}
126-
}
127-
128-
impl fmt::Display for $B {
129-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
130-
write!(f, "{}", &self.inner)
131-
}
132-
}
133-
134-
impl $O {
135-
/// Construct a new owned identifier.
136-
#[allow(clippy::new_ret_no_self)]
137-
pub fn new(value: String) -> DmResult<$O> {
138-
$check(&value, $MAX - 1)?;
139-
Ok($O { inner: value })
140-
}
141-
}
142-
143-
impl AsRef<$B> for $O {
144-
fn as_ref(&self) -> &$B {
145-
self
146-
}
147-
}
148-
149-
impl Borrow<$B> for $O {
150-
fn borrow(&self) -> &$B {
151-
self.deref()
152-
}
153-
}
154-
155-
impl Deref for $O {
156-
type Target = $B;
157-
fn deref(&self) -> &$B {
158-
$B::new(&self.inner).expect("inner satisfies all correctness criteria for $B::new")
159-
}
160-
}
161-
};
162-
}
163-
16487
/// A devicemapper name. Really just a string, but also the argument type of
16588
/// DevId::Name. Used in function arguments to indicate that the function
16689
/// takes only a name, not a devicemapper uuid.

0 commit comments

Comments
 (0)