Skip to content

Commit 3a22809

Browse files
committed
Add as_ref and check to Refinement, fix docs.
1 parent f3df653 commit 3a22809

7 files changed

+22
-5
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "refinement-types"
3-
version = "0.0.0"
3+
version = "0.1.0"
44
authors = ["nekitdev <[email protected]>"]
55
edition = "2024"
66
description = "Refinement types."

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Or by directly specifying it in the configuration like so:
2121

2222
```toml
2323
[dependencies]
24-
refinement-types = "0.0.0"
24+
refinement-types = "0.1.0"
2525
```
2626

2727
Alternatively, you can add it directly from the source:

changelogging.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[context]
22
name = "refinement-types"
3-
version = "0.0.0"
3+
version = "0.1.0"
44
url = "https://github.com/nekitdev/refinement-types"
55

66
[formats]

changes/~as-ref.feature.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `AsRef<T>` for `Refinement<T, P, C>`.

changes/~check.feature.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added `check` for `Refinement<T, P, C>` that calls `P::check` and returns `Result<(), P::Error>`.

src/core.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ impl<T: Hash, P: Predicate<T> + ?Sized, C: TypeStr + ?Sized> Hash for Refinement
145145
}
146146
}
147147

148+
impl<T, P: Predicate<T> + ?Sized, C: TypeStr + ?Sized> AsRef<T> for Refinement<T, P, C> {
149+
fn as_ref(&self) -> &T {
150+
self.get()
151+
}
152+
}
153+
148154
impl<T, P: Predicate<T> + ?Sized, C: TypeStr + ?Sized> Deref for Refinement<T, P, C> {
149155
type Target = T;
150156

@@ -350,13 +356,22 @@ impl<T, P: Predicate<T> + ?Sized, C: TypeStr + ?Sized> Refinement<T, P, C> {
350356
///
351357
/// Returns [`struct@Error`] if the value does not satisfy the predicate.
352358
pub fn refine(value: T) -> Result<Self, Error<T, P, C>> {
353-
match P::check(&value) {
359+
match Self::check(&value) {
354360
// SAFETY: the value satisfies the predicate if the check is successful
355361
Ok(()) => Ok(unsafe { Self::unchecked(value) }),
356362
Err(error) => Err(Error::new(value, error)),
357363
}
358364
}
359365

366+
/// Checks the given value.
367+
///
368+
/// # Errors
369+
///
370+
/// Returns the error produced by the predicate if the value does not satisfy it.
371+
pub fn check(value: &T) -> Result<(), P::Error> {
372+
P::check(value)
373+
}
374+
360375
/// Maps the value of the refinement.
361376
///
362377
/// # Errors

src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
#![cfg_attr(not(feature = "std"), no_std)]
1212
#![deny(missing_docs)]
13-
#![cfg_attr(docsrs, feature(auto_doc_cfg))]
13+
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
1414

1515
#[cfg(feature = "alloc")]
1616
extern crate alloc;

0 commit comments

Comments
 (0)