Comments (4)
I don't see a lot of documentation around what foreign types are
A foreign type is basically any type not defined in the crate. So, in this case, &str
is a foreign type to colored
as it is defined in the standard/core library. But it's not just foreign types. You also get the implementation on colored::ColoredString
. For example:
use colored::ColoredString;
// use colored::Colorize;
fn main() {
// this fails because the implementation of Colorize is not in scope
// but the implementation of From<&'a str> for ColoredString is in scope automatically, so we can call ColoredString::from :)
println!("{}", ColoredString::from("Hello, World!").bold());
}
My understanding of traits was that you had to manually implement them if you wanted to use them
I wouldn't say that you have to manually implement them, but they have to be implemented somewhere. For example, if you had a MyCoolStruct
that you wanted to colorize, then you would need to manually impl Colorize for MyCoolStruct
. Incidentally, you could not do something like impl Colorize for i32
in your own crate, because Rust does not allow implementations of foreign traits on foreign types (the orphan rule). In other words, it's only possible to impl Colorize for &'a str
in the colored
crate itself -- the orphan rule would actually forbid you from manually implementing Colorize
for strings.
When a trait is already implemented for a type, you only have to bring the trait into scope to get its implementation. A lot of Rust's behavior works this way. Even addition is defined by a trait. Several traits are brought into scope automatically to save you time, such as the Debug
trait, so you don't have to manually use std::fmt::Debug
to print debug info.
It's probably easier to understand with an interactive example. You should get a pretty helpful compiler error. Can you get the example to compile? 😃
For the sake of this example, you can treat inner_scope
as if it was a seperate crate.
I'm happy to continue this conversation, but I don't think this repo's issues is the right place to do it, as this conversation is starting to lean more towards the behavior of Rust's traits in general, not anything specific to colored. Feel free to open an issue in one of my repos if you have any more questions 🙂
from colored.
Thank you @spenserblack for the responses. This was very thorough.
I will close this, and ask you directly if I have any more questions about the topic.
Thank you again.
from colored.
It's the colored::Colorize
trait, which is implemented on &str
and ColoredString
. By bringing the trait into the scope of your project, you also bring into scope the implementation on foreign types (&str
).
from colored.
Thanks for the answer.
My understanding of traits was that you had to manually implement them if you wanted to use them. In this case, implementations on foreign types automatically "inherit" the implementations?
I don't see a lot of documentation around what foreign types are, and how this mechanism works around them.
from colored.
Related Issues (20)
- atty has a cve and could be replaced by is-terminal HOT 1
- No colors with less -r HOT 1
- dependency atty trips RUSTSEC-2021-0145 HOT 13
- [feature] add Join trait HOT 4
- Allow colorizing any type implementing a formatter trait HOT 3
- Implement `Add` on `ColoredString` HOT 3
- Use is_terminal from std lib? HOT 2
- Switch to std::io::IsTerminal for v3 HOT 3
- Bug: Can't compile HOT 3
- Inlined colors HOT 2
- red text appears as blue HOT 1
- Consider using snapshot tests HOT 1
- Changelog does not include version 2.1.0 yet HOT 1
- Move away from lazy_static HOT 3
- color() should support bright and custom colors with strings.
- Support for WASM HOT 2
- Undercurl / Squiggly underline / Wavy underline?
- Incorrect value of `len()` when using colored within `format!` HOT 1
- Support using the rgb crate
- Use standard LazyLock (Rust 1.80) instead of lazy_static dependencies. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from colored.