diff --git a/examples/main.rs b/examples/main.rs
index e9f4fc0..15838b4 100644
--- a/examples/main.rs
+++ b/examples/main.rs
@@ -1,7 +1,4 @@
-use bad_optics::{
- lenses::{_0, _1},
- *,
-};
+use bad_optics::lenses::*;
fn main() {
let a = ((1, 2), 3);
diff --git a/src/combinations.rs b/src/combinations.rs
index be8f024..ba8e3eb 100644
--- a/src/combinations.rs
+++ b/src/combinations.rs
@@ -1,18 +1,21 @@
-use crate::{Lens, LensOver, LensTrait, LensView};
+use crate::{
+ lenses::{LensOver, LensView},
+ Optics, OpticsTrait,
+};
#[derive(Clone, Copy)]
pub struct Combination(A, B);
-impl LensTrait for Combination {}
+impl OpticsTrait for Combination {}
-impl std::ops::Add> for Lens
+impl std::ops::Add> for Optics
where
- A: LensTrait,
- B: LensTrait,
+ A: OpticsTrait,
+ B: OpticsTrait,
{
- type Output = Lens, Lens>>;
+ type Output = Optics, Optics>>;
- fn add(self, rhs: Lens) -> Self::Output {
- Lens(Combination(self, rhs))
+ fn add(self, rhs: Optics) -> Self::Output {
+ Optics(Combination(self, rhs))
}
}
diff --git a/src/fns.rs b/src/fns.rs
index 95258e6..38055e3 100644
--- a/src/fns.rs
+++ b/src/fns.rs
@@ -1,6 +1,9 @@
-use crate::{Lens, LensOver, LensView};
+use crate::{
+ lenses::{LensOver, LensView},
+ Optics,
+};
-impl std::ops::FnOnce<(A,)> for Lens
+impl std::ops::FnOnce<(A,)> for Optics
where
L: LensView,
{
@@ -10,7 +13,7 @@ where
L::view(args.0)
}
}
-impl std::ops::FnMut<(A,)> for Lens
+impl std::ops::FnMut<(A,)> for Optics
where
L: LensView,
{
@@ -18,7 +21,7 @@ where
L::view(args.0)
}
}
-impl std::ops::Fn<(A,)> for Lens
+impl std::ops::Fn<(A,)> for Optics
where
L: LensView,
{
@@ -27,7 +30,7 @@ where
}
}
-impl std::ops::FnOnce<(A, F)> for Lens
+impl std::ops::FnOnce<(A, F)> for Optics
where
L: LensOver,
F: FnOnce(L::Field) -> L::Field,
@@ -38,7 +41,7 @@ where
L::over(args.0, args.1)
}
}
-impl std::ops::FnMut<(A, F)> for Lens
+impl std::ops::FnMut<(A, F)> for Optics
where
L: LensOver,
F: FnOnce(L::Field) -> L::Field,
@@ -47,7 +50,7 @@ where
L::over(args.0, args.1)
}
}
-impl std::ops::Fn<(A, F)> for Lens
+impl std::ops::Fn<(A, F)> for Optics
where
L: LensOver,
F: FnOnce(L::Field) -> L::Field,
diff --git a/src/lenses/first.rs b/src/lenses/first.rs
index 4615c8a..3f5b02f 100644
--- a/src/lenses/first.rs
+++ b/src/lenses/first.rs
@@ -1,9 +1,12 @@
-use crate::{Lens, LensOver, LensTrait, LensView};
+use crate::{
+ lenses::{LensOver, LensView},
+ Optics, OpticsTrait,
+};
#[derive(Clone, Copy)]
pub struct _0Inner;
-pub const _0: Lens<_0Inner> = Lens(_0Inner);
-impl LensTrait for _0Inner {}
+pub const _0: Optics<_0Inner> = Optics(_0Inner);
+impl OpticsTrait for _0Inner {}
macro_rules! make_tuples {
($f:ident, ( $( $v:ident ),* ), ( $( $t:ident ),* ) ) => {
diff --git a/src/lenses/identity.rs b/src/lenses/identity.rs
index f1bf46d..1d49fc0 100644
--- a/src/lenses/identity.rs
+++ b/src/lenses/identity.rs
@@ -1,10 +1,13 @@
-use crate::{LensOver, LensTrait, LensView};
+use crate::{
+ lenses::{LensOver, LensView},
+ OpticsTrait,
+};
#[allow(non_camel_case_types)]
#[derive(Clone, Copy)]
pub struct id;
-impl LensTrait for id {}
+impl OpticsTrait for id {}
impl LensView for id {
type Field = T;
diff --git a/src/lenses/mod.rs b/src/lenses/mod.rs
index daadf20..ce1d6dc 100644
--- a/src/lenses/mod.rs
+++ b/src/lenses/mod.rs
@@ -5,3 +5,55 @@ mod first;
pub use first::_0;
mod second;
pub use second::_1;
+
+use crate::{Optics, OpticsTrait};
+
+/// For lenses that allow viewing
+pub trait LensView: OpticsTrait {
+ type Field;
+
+ fn view(thing: T) -> Self::Field;
+}
+
+/// For lenses that allow setting
+pub trait LensOver: LensView {
+ fn over(thing: T, f: F) -> T
+ where
+ F: FnOnce(Self::Field) -> Self::Field;
+
+ fn set(thing: T, v: Self::Field) -> T {
+ Self::over(thing, |_| v)
+ }
+}
+
+impl LensView for Optics
+where
+ L: LensView,
+{
+ type Field = L::Field;
+
+ fn view(thing: T) -> Self::Field {
+ L::view(thing)
+ }
+}
+impl LensOver for Optics
+where
+ L: LensOver,
+{
+ fn over(thing: T, f: F) -> T
+ where
+ F: FnOnce(Self::Field) -> Self::Field,
+ {
+ L::over(thing, f)
+ }
+}
+
+pub fn view>(_lens: L, thing: T) -> L::Field {
+ L::view(thing)
+}
+pub fn set>(_lens: L, thing: T, v: L::Field) -> T {
+ L::set(thing, v)
+}
+pub fn over>(_lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T {
+ L::over(thing, f)
+}
diff --git a/src/lenses/result.rs b/src/lenses/result.rs
new file mode 100644
index 0000000..8b13789
--- /dev/null
+++ b/src/lenses/result.rs
@@ -0,0 +1 @@
+
diff --git a/src/lenses/second.rs b/src/lenses/second.rs
index d781d9f..d2c0d4f 100644
--- a/src/lenses/second.rs
+++ b/src/lenses/second.rs
@@ -1,9 +1,12 @@
-use crate::{Lens, LensOver, LensTrait, LensView};
+use crate::{
+ lenses::{LensOver, LensView},
+ Optics, OpticsTrait,
+};
#[derive(Clone, Copy)]
pub struct _1Inner;
-pub const _1: Lens<_1Inner> = Lens(_1Inner);
-impl LensTrait for _1Inner {}
+pub const _1: Optics<_1Inner> = Optics(_1Inner);
+impl OpticsTrait for _1Inner {}
macro_rules! make_tuples {
($f:ident, ( $( $v:ident ),* ), ( $( $t:ident ),* ) ) => {
diff --git a/src/lib.rs b/src/lib.rs
index 0682c68..fbe68bb 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,81 +1,30 @@
#![feature(unboxed_closures, fn_traits)]
-/// Base trait
-pub trait LensTrait {}
-
-/// For lenses that allow viewing
-pub trait LensView: LensTrait {
- type Field;
-
- fn view(thing: T) -> Self::Field;
-}
-
-/// For lenses that allow setting
-pub trait LensOver: LensView {
- fn over(thing: T, f: F) -> T
- where
- F: FnOnce(Self::Field) -> Self::Field;
-
- fn set(thing: T, v: Self::Field) -> T {
- Self::over(thing, |_| v)
- }
-}
-
-/// Wrapper type
-#[derive(Clone, Copy)]
-pub struct Lens(T);
-
-impl LensTrait for Lens {}
-impl LensView for Lens
-where
- L: LensView,
-{
- type Field = L::Field;
-
- fn view(thing: T) -> Self::Field {
- L::view(thing)
- }
-}
-impl LensOver for Lens
-where
- L: LensOver,
-{
- fn over(thing: T, f: F) -> T
- where
- F: FnOnce(Self::Field) -> Self::Field,
- {
- L::over(thing, f)
- }
-}
-
-pub fn view>(_lens: L, thing: T) -> L::Field {
- L::view(thing)
-}
-pub fn set>(_lens: L, thing: T, v: L::Field) -> T {
- L::set(thing, v)
-}
-pub fn over>(_lens: L, thing: T, f: impl FnOnce(L::Field) -> L::Field) -> T {
- L::over(thing, f)
-}
-
// TODO add third_from_tuple, etc
// TODO array traversals
// TODO something for structs
+// TODO something for Ok, Some, etc
+
// TODO make over work with changing types
+/// Base trait
+pub trait OpticsTrait {}
+
+/// Wrapper type
+#[derive(Clone, Copy)]
+pub struct Optics(pub(crate) T);
+impl OpticsTrait for Optics {}
+
mod combinations;
mod fns;
pub mod lenses;
#[cfg(test)]
mod tests {
- use super::{
- lenses::{_0, _1},
- *,
- };
+ use super::lenses::*;
#[test]
fn view_first_from_tuple() {