Debian Codegraph @

libgrust/libproc_macro/bridge/ident.rs

raw
1 // Copyright (C) 2023-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU Proc Macro Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // Under Section 7 of GPL version 3, you are granted additional
15 // permissions described in the GCC Runtime Library Exception, version
16 // 3.1, as published by the Free Software Foundation.
17
18 // You should have received a copy of the GNU General Public License and
19 // a copy of the GCC Runtime Library Exception along with this program;
20 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
21 // <http://www.gnu.org/licenses/>.
22
23 use bridge::ffistring::FFIString;
24 use bridge::span::Span;
25 use std::fmt;
26
27 extern "C" {
28 fn Ident__new(str: FFIString, span: Span) -> Ident;
29 fn Ident__new_raw(str: FFIString, span: Span) -> Ident;
30 fn Ident__drop(ident: *mut Ident);
31 fn Ident__clone(ident: *const Ident) -> Ident;
32 }
33
34 #[repr(C)]
35 #[derive(Debug)]
36 pub struct Ident {
37 pub(crate) is_raw: bool,
38 value: FFIString,
39 span: Span,
40 }
41
42 impl Ident {
43 pub fn new(string: &str, span: Span) -> Self {
44 unsafe { Ident__new(string.into(), span) }
45 }
46
47 pub fn new_raw(string: &str, span: Span) -> Self {
48 unsafe { Ident__new_raw(string.into(), span) }
49 }
50
51 pub fn span(&self) -> Span {
52 self.span
53 }
54
55 pub fn set_span(&mut self, span: Span) {
56 self.span = span;
57 }
58 }
59
60 impl Drop for Ident {
61 fn drop(&mut self) {
62 unsafe { Ident__drop(self as *mut Ident) }
63 }
64 }
65
66 impl fmt::Display for Ident {
67 /// Display as lossless converted string.
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 if self.is_raw {
70 f.write_str("r#")?;
71 }
72 self.value.fmt(f)
73 }
74 }
75
76 impl Clone for Ident {
77 fn clone(&self) -> Self {
78 unsafe { Ident__clone(self as *const Ident) }
79 }
80 }