Skip to content

Commit 46adb3b

Browse files
authoredMay 17, 2023
feat(const): Constify various functions (#263)
This is primarily aimed at making `SourceSpan` and `SourceOffset` usable in const contexts. Constifiable functions were found with the `clippy::missing_const_for_fn` lint, though it reported at least two false positives.
1 parent c25676c commit 46adb3b

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed
 

‎src/eyreish/ptr.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ where
4343
Box::from_raw(self.ptr.as_ptr())
4444
}
4545

46-
pub(crate) fn by_ref<'a>(&self) -> Ref<'a, T> {
46+
pub(crate) const fn by_ref<'a>(&self) -> Ref<'a, T> {
4747
Ref {
4848
ptr: self.ptr,
4949
lifetime: PhantomData,
@@ -91,7 +91,7 @@ where
9191
}
9292
}
9393

94-
pub(crate) fn from_raw(ptr: NonNull<T>) -> Self {
94+
pub(crate) const fn from_raw(ptr: NonNull<T>) -> Self {
9595
Ref {
9696
ptr,
9797
lifetime: PhantomData,
@@ -112,7 +112,7 @@ where
112112
}
113113
}
114114

115-
pub(crate) fn as_ptr(self) -> *const T {
115+
pub(crate) const fn as_ptr(self) -> *const T {
116116
self.ptr.as_ptr() as *const T
117117
}
118118

@@ -154,7 +154,7 @@ where
154154
}
155155
}
156156

157-
pub(crate) fn by_ref(self) -> Ref<'a, T> {
157+
pub(crate) const fn by_ref(self) -> Ref<'a, T> {
158158
Ref {
159159
ptr: self.ptr,
160160
lifetime: PhantomData,

‎src/handlers/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct DebugReportHandler;
1313
impl DebugReportHandler {
1414
/// Create a new [`NarratableReportHandler`](crate::NarratableReportHandler)
1515
/// There are no customization options.
16-
pub fn new() -> Self {
16+
pub const fn new() -> Self {
1717
Self
1818
}
1919
}

‎src/handlers/json.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub struct JSONReportHandler;
1313
impl JSONReportHandler {
1414
/// Create a new [`JSONReportHandler`]. There are no customization
1515
/// options.
16-
pub fn new() -> Self {
16+
pub const fn new() -> Self {
1717
Self
1818
}
1919
}
@@ -49,7 +49,7 @@ impl fmt::Display for Escape<'_> {
4949
}
5050
}
5151

52-
fn escape(input: &'_ str) -> Escape<'_> {
52+
const fn escape(input: &'_ str) -> Escape<'_> {
5353
Escape(input)
5454
}
5555

‎src/handlers/narratable.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub struct NarratableReportHandler {
2121
impl NarratableReportHandler {
2222
/// Create a new [`NarratableReportHandler`]. There are no customization
2323
/// options.
24-
pub fn new() -> Self {
24+
pub const fn new() -> Self {
2525
Self {
2626
footer: None,
2727
context_lines: 1,
@@ -31,13 +31,13 @@ impl NarratableReportHandler {
3131

3232
/// Include the cause chain of the top-level error in the report, if
3333
/// available.
34-
pub fn with_cause_chain(mut self) -> Self {
34+
pub const fn with_cause_chain(mut self) -> Self {
3535
self.with_cause_chain = true;
3636
self
3737
}
3838

3939
/// Do not include the cause chain of the top-level error in the report.
40-
pub fn without_cause_chain(mut self) -> Self {
40+
pub const fn without_cause_chain(mut self) -> Self {
4141
self.with_cause_chain = false;
4242
self
4343
}
@@ -49,7 +49,7 @@ impl NarratableReportHandler {
4949
}
5050

5151
/// Sets the number of lines of context to show around each error.
52-
pub fn with_context_lines(mut self, lines: usize) -> Self {
52+
pub const fn with_context_lines(mut self, lines: usize) -> Self {
5353
self.context_lines = lines;
5454
self
5555
}

‎src/protocol.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,10 @@ pub struct LabeledSpan {
241241

242242
impl LabeledSpan {
243243
/// Makes a new labeled span.
244-
pub fn new(label: Option<String>, offset: ByteOffset, len: usize) -> Self {
244+
pub const fn new(label: Option<String>, offset: ByteOffset, len: usize) -> Self {
245245
Self {
246246
label,
247-
span: (offset, len).into(),
247+
span: SourceSpan::new(SourceOffset(offset), SourceOffset(len)),
248248
}
249249
}
250250

@@ -310,22 +310,22 @@ impl LabeledSpan {
310310
}
311311

312312
/// Returns a reference to the inner [`SourceSpan`].
313-
pub fn inner(&self) -> &SourceSpan {
313+
pub const fn inner(&self) -> &SourceSpan {
314314
&self.span
315315
}
316316

317317
/// Returns the 0-based starting byte offset.
318-
pub fn offset(&self) -> usize {
318+
pub const fn offset(&self) -> usize {
319319
self.span.offset()
320320
}
321321

322322
/// Returns the number of bytes this `LabeledSpan` spans.
323-
pub fn len(&self) -> usize {
323+
pub const fn len(&self) -> usize {
324324
self.span.len()
325325
}
326326

327327
/// True if this `LabeledSpan` is empty.
328-
pub fn is_empty(&self) -> bool {
328+
pub const fn is_empty(&self) -> bool {
329329
self.span.is_empty()
330330
}
331331
}
@@ -422,7 +422,7 @@ pub struct MietteSpanContents<'a> {
422422

423423
impl<'a> MietteSpanContents<'a> {
424424
/// Make a new [`MietteSpanContents`] object.
425-
pub fn new(
425+
pub const fn new(
426426
data: &'a [u8],
427427
span: SourceSpan,
428428
line: usize,
@@ -440,7 +440,7 @@ impl<'a> MietteSpanContents<'a> {
440440
}
441441

442442
/// Make a new [`MietteSpanContents`] object, with a name for its 'file'.
443-
pub fn new_named(
443+
pub const fn new_named(
444444
name: String,
445445
data: &'a [u8],
446446
span: SourceSpan,
@@ -492,26 +492,26 @@ pub struct SourceSpan {
492492

493493
impl SourceSpan {
494494
/// Create a new [`SourceSpan`].
495-
pub fn new(start: SourceOffset, length: SourceOffset) -> Self {
495+
pub const fn new(start: SourceOffset, length: SourceOffset) -> Self {
496496
Self {
497497
offset: start,
498498
length: length.offset(),
499499
}
500500
}
501501

502502
/// The absolute offset, in bytes, from the beginning of a [`SourceCode`].
503-
pub fn offset(&self) -> usize {
503+
pub const fn offset(&self) -> usize {
504504
self.offset.offset()
505505
}
506506

507507
/// Total length of the [`SourceSpan`], in bytes.
508-
pub fn len(&self) -> usize {
508+
pub const fn len(&self) -> usize {
509509
self.length
510510
}
511511

512512
/// Whether this [`SourceSpan`] has a length of zero. It may still be useful
513513
/// to point to a specific point.
514-
pub fn is_empty(&self) -> bool {
514+
pub const fn is_empty(&self) -> bool {
515515
self.length == 0
516516
}
517517
}
@@ -589,7 +589,7 @@ pub struct SourceOffset(ByteOffset);
589589

590590
impl SourceOffset {
591591
/// Actual byte offset.
592-
pub fn offset(&self) -> ByteOffset {
592+
pub const fn offset(&self) -> ByteOffset {
593593
self.0
594594
}
595595

0 commit comments

Comments
 (0)
Please sign in to comment.