Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions crates/core/cgp-base-types/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
# cgp-base-types

`cgp-base-types` provides the type-level primitives of Context-Generic Programming (CGP): the
symbols that name fields, the lists and sums that describe a type's fields and variants, and the
paths that namespaces route along.
[Context-Generic Programming (CGP)](https://contextgeneric.dev/) selects trait implementations at
compile time through types, so the names it works with are types too: a field's name is a type-level
string, and a list of fields is a type-level list. `cgp-base-types` provides those primitives: the
symbols, the lists and sums that describe a type's fields and variants, and the paths that
namespaces route along. A `Product!` list, for example, is a chain of `Cons` cells closed by `Nil`:

The `cgp` crate re-exports these types under `cgp::core::base`, and the macros that build them, such
as `Symbol!` and `Product!`, under `cgp::prelude`. Most projects therefore depend on
[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest.
```rust,ignore
use cgp::core::field::types::{Cons, Nil};
use cgp::prelude::*;

To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the
[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the
reference for each construct.
// `Product![u8, String]` is the list `Cons<u8, Cons<String, Nil>>`.
fn as_cells(list: Product![u8, String]) -> Cons<u8, Cons<String, Nil>> {
list
}

fn main() {
let Cons(first, Cons(second, Nil)) = as_cells(product![1, "one".to_owned()]);
assert_eq!((first, second.as_str()), (1, "one"));
}
```

Projects reach these types through the `cgp` crate, which re-exports them under `cgp::core::base`
and the macros that build them under `cgp::prelude`, so they depend on
[`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's reference has a page
for each of its main items: [`Symbol!`](https://contextgeneric.dev/docs/reference/macros/symbol),
[`Cons`](https://contextgeneric.dev/docs/reference/types/cons),
[`Nil`](https://contextgeneric.dev/docs/reference/types/nil),
[`Chars`](https://contextgeneric.dev/docs/reference/types/chars),
[`PathCons`](https://contextgeneric.dev/docs/reference/types/path_cons),
[`StaticFormat`](https://contextgeneric.dev/docs/reference/traits/formatting/static_format), and
[`ConcatPath`](https://contextgeneric.dev/docs/reference/traits/formatting/concat_path).

To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and
the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API.
4 changes: 1 addition & 3 deletions crates/core/cgp-base-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
//! This crate provides CGP's type-level primitives: symbols, lists, sums, and paths.
//!
//! See [`Symbol!`](https://contextgeneric.dev/docs/reference/macros/symbol), [`Cons`](https://contextgeneric.dev/docs/reference/types/cons), [`Nil`](https://contextgeneric.dev/docs/reference/types/nil), [`Chars`](https://contextgeneric.dev/docs/reference/types/chars), [`PathCons`](https://contextgeneric.dev/docs/reference/types/path_cons), [`StaticFormat`](https://contextgeneric.dev/docs/reference/traits/formatting/static_format), and [`ConcatPath`](https://contextgeneric.dev/docs/reference/traits/formatting/concat_path).
#![doc = include_str!("../README.md")]
#![no_std]
#![warn(missing_docs)]

Expand Down
52 changes: 44 additions & 8 deletions crates/core/cgp-component/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
# cgp-component

`cgp-component` provides the component machinery of Context-Generic Programming (CGP): the
delegation traits that let a context choose a provider for each component, the lookup that finds
that provider, and the providers CGP ships with.
In [Context-Generic Programming (CGP)](https://contextgeneric.dev/), each context chooses, at
compile time, which implementation of a trait it uses. `cgp-component` provides the machinery behind
that choice: the delegation traits that record which provider a context chose for each component,
the lookup that reads the choice, and the providers CGP ships with. The choice is recorded in a
type-level table, which the lookup reads:

The `cgp` crate re-exports it as `cgp::core::component`. Most projects therefore depend on
[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest.
```rust,ignore
use cgp::core::component::DelegateComponent;
use cgp::prelude::*;

To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the
[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the
reference for each construct.
#[cgp_component(Greeter)]
pub trait CanGreet {
fn greet(&self) -> String;
}

#[cgp_impl(new GreetHello)]
impl Greeter {
fn greet(&self) -> String {
"Hello!".to_owned()
}
}

pub struct Person;

delegate_components! { Person { GreeterComponent: GreetHello } }

fn main() {
// The wiring is a type-level table: this entry names `GreetHello`.
let _provider: <Person as DelegateComponent<GreeterComponent>>::Delegate = GreetHello;
assert_eq!(Person.greet(), "Hello!");
}
```

Projects reach this machinery through the `cgp` crate, which re-exports it as
`cgp::core::component`, so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the
example does. The website's reference has a page for each of its main items:
[`DelegateComponent`](https://contextgeneric.dev/docs/reference/traits/wiring/delegate_component),
[`IsProviderFor`](https://contextgeneric.dev/docs/reference/traits/wiring/is_provider_for),
[`CanUseComponent`](https://contextgeneric.dev/docs/reference/traits/wiring/can_use_component),
[`UseContext`](https://contextgeneric.dev/docs/reference/providers/use_context),
[`UseDefault`](https://contextgeneric.dev/docs/reference/providers/use_default),
[`UseDelegate`](https://contextgeneric.dev/docs/reference/providers/use_delegate), and
[`RedirectLookup`](https://contextgeneric.dev/docs/reference/providers/redirect_lookup).

To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and
the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API.
5 changes: 1 addition & 4 deletions crates/core/cgp-component/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![doc = include_str!("../README.md")]
#![no_std]
//! This crate provides CGP's component machinery: delegation, provider lookup, and the built-in
//! providers.
//!
//! See [`DelegateComponent`](https://contextgeneric.dev/docs/reference/traits/wiring/delegate_component), [`IsProviderFor`](https://contextgeneric.dev/docs/reference/traits/wiring/is_provider_for), [`CanUseComponent`](https://contextgeneric.dev/docs/reference/traits/wiring/can_use_component), [`UseContext`](https://contextgeneric.dev/docs/reference/providers/use_context), [`UseDefault`](https://contextgeneric.dev/docs/reference/providers/use_default), [`UseDelegate`](https://contextgeneric.dev/docs/reference/providers/use_delegate), and [`RedirectLookup`](https://contextgeneric.dev/docs/reference/providers/redirect_lookup).
#![warn(missing_docs)]

/// Re-exports the component machinery that the macros expand to.
Expand Down
50 changes: 42 additions & 8 deletions crates/core/cgp-error/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
# cgp-error

`cgp-error` provides the error components of Context-Generic Programming (CGP): an error type that
each context chooses, and the components that raise errors into it and wrap them with detail.
[Context-Generic Programming (CGP)](https://contextgeneric.dev/) lets each context choose how a
trait is implemented, and its error handling extends that choice to the error type itself.
`cgp-error` provides the components behind it: the abstract error type, and the components that
raise errors into it and wrap them with detail. Generic code raises an error without knowing its
type, and each context decides what it becomes:

The `cgp` crate re-exports it as `cgp::core::error`, and its traits `HasErrorType`, `CanRaiseError`,
and `CanWrapError` under `cgp::prelude`. Most projects therefore depend on
[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest.
```rust,ignore
use cgp::core::error::{ErrorRaiserComponent, ErrorTypeProviderComponent};
use cgp::extra::error::RaiseFrom;
use cgp::prelude::*;

To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the
[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the
reference for each construct.
pub fn parse_port<Context>(input: &str) -> Result<u16, Context::Error>
where
Context: CanRaiseError<String>,
{
input.parse().map_err(|_| Context::raise_error(format!("not a port: {input}")))
}

pub struct App;

delegate_components! {
App {
open ErrorRaiserComponent;

ErrorTypeProviderComponent: UseType<String>,
@ErrorRaiserComponent.String: RaiseFrom,
}
}

fn main() {
assert_eq!(parse_port::<App>("80"), Ok(80));
assert_eq!(parse_port::<App>("http"), Err("not a port: http".to_owned()));
}
```

Projects reach these components through the `cgp` crate, which re-exports them as
`cgp::core::error`, so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example
does. The website's reference has a page for each of its main items:
[`HasErrorType`](https://contextgeneric.dev/docs/reference/components/has_error_type),
[`CanRaiseError`](https://contextgeneric.dev/docs/reference/components/can_raise_error), and
[`CanWrapError`](https://contextgeneric.dev/docs/reference/components/can_wrap_error).

To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and
the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API.
4 changes: 1 addition & 3 deletions crates/core/cgp-error/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![doc = include_str!("../README.md")]
#![no_std]
//! This crate provides CGP's error components: the abstract error type, raising, and wrapping.
//!
//! See [`HasErrorType`](https://contextgeneric.dev/docs/reference/components/has_error_type), [`CanRaiseError`](https://contextgeneric.dev/docs/reference/components/can_raise_error), and [`CanWrapError`](https://contextgeneric.dev/docs/reference/components/can_wrap_error).
#![warn(missing_docs)]

mod contexts;
Expand Down
39 changes: 30 additions & 9 deletions crates/core/cgp-field/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
# cgp-field

`cgp-field` provides the field access of Context-Generic Programming (CGP): the traits that read and
build a type's fields by name, the tags that name them, and the structural casts between records and
between enums.
A [Context-Generic Programming (CGP)](https://contextgeneric.dev/) provider reads the values it
needs from its context's fields by name, so it fits any context that has those fields. `cgp-field`
provides that field access: the traits that read and build a type's fields by name, the tags that
name them, and the structural casts between records and between enums. `#[derive(HasField)]` gives
each field a tag, and the field can then be read by name:

The `cgp` crate re-exports it as `cgp::core::field`, and the derives that implement its traits under
`cgp::prelude`. Most projects therefore depend on [`cgp`](https://crates.io/crates/cgp) alone,
without adding this crate to their manifest.
```rust,ignore
use cgp::core::field::traits::HasField;
use cgp::prelude::*;

To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the
[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the
reference for each construct.
#[derive(HasField)]
pub struct Person {
pub name: String,
pub age: u8,
}

fn main() {
let person = Person { name: "Alice".to_owned(), age: 30 };
assert_eq!(person.get_field(PhantomData::<Symbol!("age")>), &30);
}
```

Projects reach this field access through the `cgp` crate, which re-exports it as `cgp::core::field`,
so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's
reference has a page for each of its main items:
[`HasField`](https://contextgeneric.dev/docs/reference/traits/field-access/has_field),
[`Index`](https://contextgeneric.dev/docs/reference/types/index_type),
[`MRef`](https://contextgeneric.dev/docs/reference/types/mref), and the
[casts](https://contextgeneric.dev/docs/reference/traits/casting/).

To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and
the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API.
5 changes: 1 addition & 4 deletions crates/core/cgp-field/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![doc = include_str!("../README.md")]
#![no_std]
//! This crate provides CGP's field access: the field traits, the field tags, the type-level lists,
//! and the structural casts.
//!
//! See [`HasField`](https://contextgeneric.dev/docs/reference/traits/field-access/has_field), [`Index`](https://contextgeneric.dev/docs/reference/types/index_type), [`MRef`](https://contextgeneric.dev/docs/reference/types/mref), and the [casts](https://contextgeneric.dev/docs/reference/traits/casting/).
#![warn(missing_docs)]

/// The field traits' implementations and the built-in providers.
Expand Down
37 changes: 30 additions & 7 deletions crates/core/cgp-type/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
# cgp-type

`cgp-type` provides the abstract types of Context-Generic Programming (CGP), which let each context
choose a type that generic code names only through a trait.
[Context-Generic Programming (CGP)](https://contextgeneric.dev/) lets each context choose a trait's
implementation at compile time, and it can choose a type the same way. `cgp-type` provides these
abstract types, which generic code names only through a trait while each context picks the concrete
type in its wiring:

The `cgp` crate re-exports it as `cgp::core::types`. Most projects therefore depend on
[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest.
```rust,ignore
use cgp::prelude::*;

To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the
[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the
reference for each construct.
#[cgp_type]
pub trait HasNameType {
type Name;
}

pub struct App;

delegate_components! { App { NameTypeProviderComponent: UseType<String> } }

fn main() {
let name: <App as HasNameType>::Name = String::from("app");
assert_eq!(name, "app");
}
```

Projects reach these types through the `cgp` crate, which re-exports them as `cgp::core::types`, so
they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the example does. The website's
reference has a page for each of its main items:
[`HasType`](https://contextgeneric.dev/docs/reference/components/has_type),
[`UseType`](https://contextgeneric.dev/docs/reference/providers/use_type), and
[`UseDelegatedType`](https://contextgeneric.dev/docs/reference/providers/use_delegated_type).

To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and
the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API.
4 changes: 1 addition & 3 deletions crates/core/cgp-type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#![doc = include_str!("../README.md")]
#![no_std]
//! This crate provides CGP's abstract types: a type the context chooses behind a trait.
//!
//! See [`HasType`](https://contextgeneric.dev/docs/reference/components/has_type), [`UseType`](https://contextgeneric.dev/docs/reference/providers/use_type), and [`UseDelegatedType`](https://contextgeneric.dev/docs/reference/providers/use_delegated_type).
#![warn(missing_docs)]

mod impls;
Expand Down
70 changes: 62 additions & 8 deletions crates/extra/cgp-dispatch/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,66 @@
# cgp-dispatch

`cgp-dispatch` provides the dispatch combinators of Context-Generic Programming (CGP), which split a
value between handlers. They match an enum's variants or extract a record's fields and pass each one
to its own handler, or build a record from what the handlers return.
In [Context-Generic Programming (CGP)](https://contextgeneric.dev/), a computation can be a
component like any other, and `cgp-dispatch` provides the combinators that split a value between
several of them: they match an enum's variants or extract a record's fields and pass each part to
its own handler, or build a record from what the handlers return. Here each variant of an enum gets
its own area computation:

The `cgp` crate re-exports it as `cgp::extra::dispatch`. Most projects therefore depend on
[`cgp`](https://crates.io/crates/cgp) alone, without adding this crate to their manifest.
```rust,ignore
use cgp::extra::dispatch::MatchWithValueHandlers;
use cgp::extra::handler::{CanCompute, Computer, ComputerComponent};
use cgp::prelude::*;

To learn more, read the [`cgp` crate's documentation](https://docs.rs/cgp) and the
[CGP website](https://contextgeneric.dev/), which covers everything from a first program to the
reference for each construct.
pub struct Circle {
pub radius: f64,
}

pub struct Rectangle {
pub width: f64,
pub height: f64,
}

#[derive(CgpData)]
pub enum Shape {
Circle(Circle),
Rectangle(Rectangle),
}

#[cgp_new_provider]
impl<Context, Code> Computer<Context, Code, Circle> for ComputeArea {
type Output = f64;

fn compute(_context: &Context, _code: PhantomData<Code>, circle: Circle) -> f64 {
3.0 * circle.radius * circle.radius
}
}

#[cgp_provider]
impl<Context, Code> Computer<Context, Code, Rectangle> for ComputeArea {
type Output = f64;

fn compute(_context: &Context, _code: PhantomData<Code>, rectangle: Rectangle) -> f64 {
rectangle.width * rectangle.height
}
}

pub struct App;

delegate_components! { App { ComputerComponent: MatchWithValueHandlers<ComputeArea> } }

fn main() {
let shape = Shape::Rectangle(Rectangle { width: 2.0, height: 3.0 });
assert_eq!(App.compute(PhantomData::<()>, shape), 6.0);
}
```

Projects reach these combinators through the `cgp` crate, which re-exports them as
`cgp::extra::dispatch`, so they depend on [`cgp`](https://crates.io/crates/cgp) alone, as the
example does. The website's reference has a page for each of its main items:
[`MatchWithHandlers`](https://contextgeneric.dev/docs/reference/providers/dispatch/match_with_handlers),
[`MatchWithValueHandlers`](https://contextgeneric.dev/docs/reference/providers/dispatch/match_with_value_handlers),
and
[`ExtractFieldAndHandle`](https://contextgeneric.dev/docs/reference/providers/dispatch/extract_field_and_handle).

To learn CGP itself, the [CGP website](https://contextgeneric.dev/) starts from a first program, and
the [`cgp` crate's documentation](https://docs.rs/cgp) covers the whole API.
5 changes: 1 addition & 4 deletions crates/extra/cgp-dispatch/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#![doc = include_str!("../README.md")]
#![no_std]
#![allow(clippy::needless_lifetimes)]
//! This crate provides CGP's dispatch providers, which match, extract, and build a value on the way
//! to a handler.
//!
//! See [`MatchWithHandlers`](https://contextgeneric.dev/docs/reference/providers/dispatch/match_with_handlers), [`MatchWithValueHandlers`](https://contextgeneric.dev/docs/reference/providers/dispatch/match_with_value_handlers), and [`ExtractFieldAndHandle`](https://contextgeneric.dev/docs/reference/providers/dispatch/extract_field_and_handle).
#![warn(missing_docs)]

mod providers;
Expand Down
Loading
Loading