Skip to content

Commit d0a5241

Browse files
committed
Seal RowIndex
1 parent 9386659 commit d0a5241

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

postgres-shared/src/rows.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@ use std::io;
55
use std::ops::Range;
66

77
use stmt::Column;
8+
use rows::sealed::Sealed;
89

9-
/// A trait implemented by types that can index into columns of a row.
10-
pub trait RowIndex {
11-
/// Returns the index of the appropriate column, or `None` if no such
12-
/// column exists.
13-
fn idx(&self, stmt: &[Column]) -> Option<usize>;
10+
mod sealed {
11+
use stmt::Column;
12+
13+
pub trait Sealed {
14+
fn idx(&self, stmt: &[Column]) -> Option<usize>;
15+
}
1416
}
1517

16-
impl RowIndex for usize {
18+
/// A trait implemented by types that can index into columns of a row.
19+
///
20+
/// This cannot be implemented outside of this crate.
21+
pub trait RowIndex: Sealed {}
22+
23+
impl Sealed for usize {
1724
#[inline]
1825
fn idx(&self, stmt: &[Column]) -> Option<usize> {
1926
if *self >= stmt.len() {
@@ -24,7 +31,9 @@ impl RowIndex for usize {
2431
}
2532
}
2633

27-
impl<'a> RowIndex for str {
34+
impl RowIndex for usize {}
35+
36+
impl Sealed for str {
2837
#[inline]
2938
fn idx(&self, stmt: &[Column]) -> Option<usize> {
3039
if let Some(idx) = stmt.iter().position(|d| d.name() == self) {
@@ -40,16 +49,24 @@ impl<'a> RowIndex for str {
4049
}
4150
}
4251

43-
impl<'a, T: ?Sized> RowIndex for &'a T
52+
impl RowIndex for str {}
53+
54+
impl<'a, T> Sealed for &'a T
4455
where
45-
T: RowIndex,
56+
T: ?Sized + Sealed,
4657
{
4758
#[inline]
4859
fn idx(&self, columns: &[Column]) -> Option<usize> {
4960
T::idx(*self, columns)
5061
}
5162
}
5263

64+
impl<'a, T> RowIndex for &'a T
65+
where
66+
T: ?Sized + Sealed,
67+
{
68+
}
69+
5370
#[doc(hidden)]
5471
pub struct RowData {
5572
body: DataRowBody,

0 commit comments

Comments
 (0)