teeny_core/device/context.rs
1/*
2 * Copyright (c) 2026 Teenygrad.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use alloc::vec::Vec;
18
19use crate::{device::Device, errors::Result};
20
21/// A device's identifying metadata.
22pub trait DeviceInfo: Sized {
23 /// This device's ID type.
24 type Id;
25
26 /// This device's ID.
27 fn id(&self) -> Self::Id;
28 /// This device's name.
29 fn name(&self) -> &str;
30}
31
32/// The entry point for discovering and opening devices.
33pub trait Context<'a> {
34 /// The device type this context opens.
35 type Device: Device<'a>;
36 /// The device-info type returned by [`Context::list_devices`].
37 type DeviceInfo: DeviceInfo;
38
39 /// Lists all available devices.
40 fn list_devices(&self) -> Result<Vec<Self::DeviceInfo>>;
41
42 /// Opens the device with the given ID.
43 fn device(&self, id: &<Self::DeviceInfo as DeviceInfo>::Id) -> Result<Self::Device>;
44}